<?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::create('bank_cards', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->string('bank_name', 100)->nullable();
            $table->string('card_number', 19)->nullable();
            $table->string('shaba', 26);
            $table->string('account_holder_name', 120);
            $table->boolean('is_default')->default(false);
            $table->string('status', 30)->default('active');
            $table->json('meta')->nullable();
            $table->timestamps();

            $table->unique(['user_id', 'shaba']);
            $table->index(['user_id', 'is_default']);
        });

        Schema::table('withdrawal_requests', function (Blueprint $table) {
            $table->foreignId('bank_card_id')->nullable()->after('user_id')->constrained('bank_cards')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('withdrawal_requests', function (Blueprint $table) {
            $table->dropConstrainedForeignId('bank_card_id');
        });

        Schema::dropIfExists('bank_cards');
    }
};
