<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    public function up(): void
    {
        $brand = 'NewKipp Microfinance Solution';

        DB::table('system_settings')->updateOrInsert(
            ['key' => 'platform_name'],
            ['value' => $brand],
        );

        foreach (['mail_from_name', 'sms_from', 'whatsapp_from'] as $key) {
            DB::table('system_settings')
                ->where('key', $key)
                ->whereIn('value', ['MicroFinance SaaS', 'Microfinance SaaS', 'MF SaaS', 'MicroFinance'])
                ->update(['value' => $brand]);
        }

        DB::table('system_settings')
            ->where('key', 'bank_details')
            ->where('value', 'like', '%MicroFinance SaaS%')
            ->update([
                'value' => DB::raw("REPLACE(value, 'MicroFinance SaaS', 'NewKipp Microfinance Solution')"),
            ]);

        DB::table('system_settings')
            ->where('key', 'homepage_about_body')
            ->where('value', 'like', '%MicroFinance SaaS%')
            ->update([
                'value' => DB::raw("REPLACE(value, 'MicroFinance SaaS', 'NewKipp Microfinance Solution')"),
            ]);
    }

    public function down(): void
    {
        DB::table('system_settings')
            ->where('key', 'platform_name')
            ->where('value', 'NewKipp Microfinance Solution')
            ->update(['value' => 'MicroFinance SaaS']);
    }
};
