<?php

declare(strict_types=1);

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('identity_verifications', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('tenant_id')->nullable()->constrained('tenants')->nullOnDelete();
            $table->foreignUuid('user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->enum('provider', ['nice', 'siren24', 'kcb']);
            $table->string('request_id', 191);
            $table->text('ci')->nullable();
            $table->text('di')->nullable();
            $table->string('ci_hash', 191)->nullable();
            $table->string('di_hash', 191)->nullable();
            $table->string('name', 50)->nullable();
            $table->date('birthdate')->nullable();
            $table->enum('gender', ['male', 'female', 'other'])->nullable();
            $table->string('mobile', 20)->nullable();
            $table->enum('carrier', ['skt', 'kt', 'lgu', 'skt-mvno', 'kt-mvno', 'lgu-mvno'])->nullable();
            $table->enum('status', ['pending', 'verified', 'failed', 'expired'])->default('pending');
            $table->timestamp('expired_at')->nullable();
            $table->timestamp('verified_at')->nullable();
            $table->timestamps();

            $table->unique(['provider', 'request_id']);
            $table->index(['tenant_id', 'ci_hash']);
            $table->index(['tenant_id', 'di_hash']);
            $table->index(['tenant_id', 'status']);
        });
    }

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