<?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('documents', function (Blueprint $table) {
            $table->id();
            $table->foreignId('business_id')->constrained()->cascadeOnDelete();
            $table->enum('document_type', [
                'SalesInvoice', 'PurchaseInvoice', 'BankStatement', 'OtherDocuments', 'Other'
            ]);
            $table->string('file_name');
            $table->string('file_url');
            $table->string('mime_type')->nullable();
            $table->unsignedBigInteger('file_size')->nullable();
            $table->enum('status', [
                'Uploaded', 'Data Entered', 'Processing', 'Processed', 'Exported', 'Review', 'Deleted'
            ])->default('Uploaded');
            $table->foreignId('uploaded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('uploaded_date')->nullable();
            $table->foreignId('processed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('processed_date')->nullable();
            $table->foreignId('contact_id')->nullable()->constrained('contacts')->nullOnDelete();
            $table->boolean('is_deleted')->default(false);
            $table->timestamps();
            $table->index(['business_id', 'document_type', 'status']);
            $table->index(['business_id', 'is_deleted']);
        });
    }

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