<?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('incentive_applications', function (Blueprint $table) {
            $table->id();
            $table->string('code', 10)->unique();
            $table->foreignId('user_id')->constrained('users');
            $table->foreignId('incentive_id')->constrained('fiscal_incentives');
            $table->foreignId('proposal_id')->nullable()->constrained('investment_proposals');
            $table->enum('status', ['pendente', 'aprovada', 'recusada'])->default('pendente');
            $table->text('justification')->nullable();
            $table->text('admin_response')->nullable();
            $table->foreignId('reviewed_by')->nullable()->constrained('users');
            $table->timestamp('reviewed_at')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->unique(['user_id', 'incentive_id', 'proposal_id']);
            $table->index(['user_id', 'status']);
        });
    }

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