<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('addon_settings')) {
            return;
        }

        if (DB::table('addon_settings')->where('key_name', 'bangla_qr')->exists()) {
            return;
        }

        $columns = array_flip(Schema::getColumnListing('addon_settings'));
        $values = [
            'gateway' => 'bangla_qr',
            'mode' => 'test',
            'status' => '0',
            'provider' => 'sslcommerz',
            'merchant_id' => '',
            'terminal_id' => '',
            'store_id' => '',
            'api_key' => '',
            'initiate_url' => '',
            'status_url' => '',
            'callback_secret' => '',
        ];

        $row = [
            'id' => (string) Str::uuid(),
            'key_name' => 'bangla_qr',
            'live_values' => json_encode($values),
            'test_values' => json_encode($values),
            'settings_type' => 'payment_config',
            'mode' => 'test',
            'is_active' => 0,
            'additional_data' => json_encode([
                'gateway_title' => 'Bangla QR',
                'gateway_image' => '',
                'storage' => 'public',
            ]),
            'created_at' => now(),
            'updated_at' => now(),
        ];

        DB::table('addon_settings')->insert(array_intersect_key($row, $columns));
    }

    public function down(): void
    {
        if (Schema::hasTable('addon_settings')) {
            DB::table('addon_settings')->where('key_name', 'bangla_qr')->delete();
        }
    }
};
