<?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('users', function (Blueprint $table): void {
            $table->string('kyc_status', 40)->default('approved');
            $table->text('kyc_rejection_reason')->nullable();
        });

        Schema::create('user_kyc_profiles', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('legal_first_name');
            $table->string('legal_last_name');
            $table->date('date_of_birth');
            $table->string('document_type', 32);
            $table->string('document_number');
            $table->string('document_issuing_country', 2);
            $table->string('country_of_residence', 2);
            $table->text('address_line1');
            $table->text('address_line2')->nullable();
            $table->string('city');
            $table->string('region_state');
            $table->string('postal_code');
            $table->string('occupation')->nullable();

            $table->string('document_front_path');
            $table->string('document_back_path')->nullable();
            $table->string('selfie_document_path')->nullable();

            $table->timestamp('reviewed_at')->nullable();
            $table->foreignId('reviewed_by')->nullable()->constrained('users')->nullOnDelete();

            $table->timestamps();

            $table->unique('user_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('user_kyc_profiles');

        Schema::table('users', function (Blueprint $table): void {
            $table->dropColumn(['kyc_status', 'kyc_rejection_reason']);
        });
    }
};
