<?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('user_verifications', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();

            $table->enum('type', ['email', 'phone', 'identity', 'payment', 'business']);
            $table->enum('status', ['unverified', 'pending', 'verified', 'failed', 'expired'])
                ->default('unverified');

            // e.g. stripe_identity, twilio -- kept generic so providers are swappable.
            $table->string('provider', 64)->nullable();
            $table->string('provider_reference')->nullable();

            /*
             * Identity evidence lives on the private S3 disk. Only the object
             * path is stored, and it is never returned by a public endpoint.
             */
            $table->string('evidence_disk', 32)->nullable();
            $table->string('evidence_path')->nullable();

            $table->json('metadata')->nullable();
            $table->timestamp('submitted_at')->nullable();
            $table->timestamp('verified_at')->nullable();
            $table->timestamp('expires_at')->nullable();
            $table->string('failure_reason')->nullable();

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

            $table->timestamps();

            // One live record per verification kind per user.
            $table->unique(['user_id', 'type']);
            $table->index(['status', 'type']);
            $table->index('provider_reference');
        });
    }

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