<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('budgets', function (Blueprint $table) {
            // Adicionar centro de custos (opcional)
            $table->unsignedBigInteger('cost_center_id')->nullable()->after('subcategory_id');
            $table->foreign('cost_center_id')->references('id')->on('cost_centers')->onDelete('cascade');
        });

        // Atualizar o enum de period_type para incluir bimestral, trimestral, semestral
        DB::statement("ALTER TABLE budgets MODIFY COLUMN period_type ENUM('monthly', 'bimestral', 'trimestral', 'semestral', 'yearly') NOT NULL DEFAULT 'monthly'");
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('budgets', function (Blueprint $table) {
            $table->dropForeign(['cost_center_id']);
            $table->dropColumn('cost_center_id');
        });

        // Voltar ao enum original
        DB::statement("ALTER TABLE budgets MODIFY COLUMN period_type ENUM('monthly', 'yearly') NOT NULL DEFAULT 'monthly'");
    }
};
