<?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('citizenship_country_settings', function (Blueprint $table) {
            $table->id();
            $table->char('country_code', 2)->unique();
            $table->boolean('individual_id_enabled')->default(false);
            $table->boolean('individual_id_required')->default(false);
            $table->boolean('company_id_enabled')->default(false);
            $table->boolean('company_id_required')->default(false);
            $table->boolean('document_upload_enabled')->default(false);
            $table->boolean('document_upload_required')->default(false);
            $table->boolean('api_verification_enabled')->default(false);
            $table->string('verification_module', 50)->nullable();
            $table->string('api_provider', 50)->nullable();
            $table->json('api_provider_config')->nullable();
            $table->json('field_settings')->nullable();
            $table->timestamps();
        });

        Schema::create('client_verifications', function (Blueprint $table) {
            $table->id();
            $table->foreignId('client_id')->constrained()->onDelete('cascade');
            $table->enum('type', ['individual', 'company']);
            $table->text('id_number')->nullable();
            $table->longText('verified_data')->nullable();
            $table->enum('verification_method', ['format', 'api', 'document', 'manual']);
            $table->enum('status', ['pending', 'verified', 'rejected', 'expired'])->default('pending');
            $table->string('document_path', 500)->nullable();
            $table->string('document_type', 50)->nullable();
            $table->json('api_response')->nullable();
            $table->timestamp('verified_at')->nullable();
            $table->foreignId('verified_by')->nullable()->constrained('admins')->nullOnDelete();
            $table->text('rejected_reason')->nullable();
            $table->timestamp('expires_at')->nullable();
            $table->timestamps();

            $table->index(['client_id', 'status']);
            $table->index(['status', 'created_at']);
        });

    }

    public function down(): void
    {
        Schema::dropIfExists('client_verifications');
        Schema::dropIfExists('citizenship_country_settings');
    }
};
