<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('manual_payment_gateways', function (Blueprint $table) {
            $table->string('slug')->nullable()->unique()->after('id');
            $table->text('description_ar')->nullable()->after('name_en');
            $table->text('description_en')->nullable()->after('description_ar');
            $table->json('receiver_data')->nullable()->after('account_number');
            $table->json('required_fields')->nullable()->after('receiver_data');
            $table->json('supported_currencies')->nullable()->after('required_fields');
            $table->unsignedBigInteger('min_amount_minor')->nullable()->after('supported_currencies');
            $table->unsignedBigInteger('max_amount_minor')->nullable()->after('min_amount_minor');
            $table->string('fee_type')->default('none')->after('max_amount_minor');
            $table->unsignedInteger('fee_value')->default(0)->after('fee_type');
            $table->boolean('requires_proof')->default(true)->after('fee_value');
            $table->boolean('proof_is_required')->default(true)->after('requires_proof');
            $table->unsignedInteger('proof_max_mb')->default(6)->after('proof_is_required');
            $table->text('proof_instructions_ar')->nullable()->after('proof_max_mb');
            $table->text('proof_instructions_en')->nullable()->after('proof_instructions_ar');
            $table->unsignedInteger('review_sla_minutes')->default(60)->after('proof_instructions_en');
            $table->unsignedInteger('expires_after_minutes')->default(1440)->after('review_sla_minutes');
            $table->json('country_codes')->nullable()->after('expires_after_minutes');
            $table->json('risk_rules')->nullable()->after('country_codes');
        });

        Schema::table('manual_payment_submissions', function (Blueprint $table) {
            $table->string('submission_type')->default('wallet_topup')->after('order_id')->index();
            $table->string('reference_number')->nullable()->after('payer_phone')->index();
            $table->string('payer_identifier_type')->nullable()->after('reference_number');
            $table->string('payer_identifier')->nullable()->after('payer_identifier_type')->index();
            $table->json('dynamic_fields')->nullable()->after('payer_identifier');
            $table->string('proof_hash')->nullable()->after('proof_path')->index();
            $table->unsignedTinyInteger('risk_score')->default(0)->after('proof_hash');
            $table->json('risk_flags')->nullable()->after('risk_score');
            $table->text('review_note')->nullable()->after('notes');
            $table->text('rejection_reason')->nullable()->after('review_note');
            $table->timestamp('additional_info_requested_at')->nullable()->after('rejection_reason');
            $table->timestamp('expires_at')->nullable()->after('additional_info_requested_at');
        });
    }

    public function down(): void
    {
        Schema::table('manual_payment_submissions', function (Blueprint $table) {
            $table->dropColumn([
                'submission_type',
                'reference_number',
                'payer_identifier_type',
                'payer_identifier',
                'dynamic_fields',
                'proof_hash',
                'risk_score',
                'risk_flags',
                'review_note',
                'rejection_reason',
                'additional_info_requested_at',
                'expires_at',
            ]);
        });

        Schema::table('manual_payment_gateways', function (Blueprint $table) {
            $table->dropUnique(['slug']);
            $table->dropColumn([
                'slug',
                'description_ar',
                'description_en',
                'receiver_data',
                'required_fields',
                'supported_currencies',
                'min_amount_minor',
                'max_amount_minor',
                'fee_type',
                'fee_value',
                'requires_proof',
                'proof_is_required',
                'proof_max_mb',
                'proof_instructions_ar',
                'proof_instructions_en',
                'review_sla_minutes',
                'expires_after_minutes',
                'country_codes',
                'risk_rules',
            ]);
        });
    }
};
