<?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('data_protection_audits', function (Blueprint $table) {
            $table->id();
            $table->foreignId('solicitor_id')->constrained()->cascadeOnDelete();
            $table->foreignId('recording_id')->unique()->constrained()->cascadeOnDelete();
            $table->foreignId('communication_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('call_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('task_id')->nullable()->constrained('tasks')->nullOnDelete();
            $table->string('status')->default('processing');
            $table->string('result')->nullable();
            $table->unsignedTinyInteger('number_of_checks_confirmed')->nullable();
            $table->json('checks_confirmed')->nullable();
            $table->boolean('completed_at_outset')->nullable();
            $table->json('evidence')->nullable();
            $table->text('reasoning')->nullable();
            $table->longText('raw_output')->nullable();
            $table->text('error')->nullable();
            $table->timestamps();

            $table->index(['solicitor_id', 'status']);
            $table->index(['solicitor_id', 'result']);
        });
    }

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