<?php

declare(strict_types=1);

use App\Models\ConsentRecord;
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('consent_records', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignUuid('user_id')->constrained('users')->cascadeOnDelete();
            $table->string('purpose', 80);
            $table->enum('jurisdiction', [
                ConsentRecord::JURISDICTION_PIPA,
                ConsentRecord::JURISDICTION_GDPR,
                ConsentRecord::JURISDICTION_CCPA,
            ]);
            $table->string('version', 30);
            $table->boolean('granted');
            $table->string('ip', 45)->nullable();
            $table->string('ua', 512)->nullable();
            $table->timestamp('recorded_at')->useCurrent();

            $table->index(['user_id', 'purpose', 'recorded_at']);
            $table->index(['tenant_id', 'jurisdiction']);
        });
    }

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