<?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('investment_proposals', function (Blueprint $table) {
            $table->id();
            $table->string('code', 10)->unique();
            $table->string('name');
            $table->enum('sector', [
                'infraestrutura', 'agricultura', 'turismo', 'energia',
                'pescas', 'tecnologia', 'comercio', 'industria',
                'mineracao', 'saude', 'educacao'
            ]);
            $table->text('description');
            $table->enum('status', [
                'rascunho', 'submetido', 'em_analise', 'aprovado', 'rejeitado', 'em_revisao'
            ])->default('rascunho');
            $table->decimal('investment_value', 20, 2);
            $table->integer('jobs_to_create')->default(0);
            $table->date('start_date')->nullable();
            $table->integer('duration_months')->nullable();
            $table->decimal('own_capital_pct', 5, 2)->default(0);
            $table->decimal('bank_credit_pct', 5, 2)->default(0);
            $table->decimal('external_investor_pct', 5, 2)->default(0);
            $table->foreignId('province_id')->nullable()->constrained('provinces');
            $table->foreignId('district_id')->nullable()->constrained('districts');
            $table->text('address')->nullable();
            $table->boolean('is_special_economic_zone')->default(false);
            $table->foreignId('user_id')->constrained('users');
            $table->foreignId('opportunity_id')->nullable()->constrained('investment_opportunities');
            $table->string('business_plan_url')->nullable();
            $table->string('financial_projections_url')->nullable();
            $table->json('additional_documents')->nullable();
            $table->decimal('progress', 5, 2)->default(0);
            $table->json('stages')->nullable();
            $table->json('institutions_involved')->nullable();
            $table->text('rejection_reason')->nullable();
            $table->text('admin_notes')->nullable();
            $table->foreignId('reviewed_by')->nullable()->constrained('users');
            $table->timestamp('submitted_at')->nullable();
            $table->timestamp('reviewed_at')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->index(['status', 'sector']);
            $table->index(['user_id', 'status']);
            $table->index(['reviewed_by', 'status']);
            $table->index('code');
            $table->index('submitted_at');
        });
    }

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