<?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('regulation_documents', function (Blueprint $table) {
            $table->id();
            $table->foreignId('state_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('city_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('uploaded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->string('title');                         // e.g. "Mumbai DCR 2034"
            $table->string('document_type');                 // pdf | docx
            $table->string('original_filename');
            $table->string('storage_path');                  // path in storage
            $table->string('file_hash')->nullable();         // SHA-256 for dedup
            $table->unsignedBigInteger('file_size_bytes')->default(0);
            // AI processing
            $table->enum('extraction_status', ['pending','processing','completed','failed'])->default('pending');
            $table->text('extraction_error')->nullable();
            $table->json('extracted_text_chunks')->nullable(); // OCR / parsed text
            $table->json('extracted_rules')->nullable();       // AI-structured zone rules
            $table->json('ai_metadata')->nullable();           // tokens used, model, etc.
            // Human review
            $table->enum('review_status', ['pending','approved','rejected'])->default('pending');
            $table->foreignId('reviewed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('reviewed_at')->nullable();
            $table->text('review_notes')->nullable();
            $table->boolean('rules_applied')->default(false); // whether rules were pushed to zone_rules
            $table->timestamps();
        });
    }

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