<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('budgets', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('category_id')->nullable()->constrained()->onDelete('cascade');
            $table->string('name')->nullable(); // Para presupuestos personalizados sin categoría
            $table->decimal('amount', 15, 2);
            $table->string('currency', 3)->default('EUR');
            $table->integer('year');
            $table->integer('month'); // 1-12, null para presupuesto anual
            $table->enum('period_type', ['monthly', 'yearly'])->default('monthly');
            $table->boolean('is_active')->default(true);
            $table->text('notes')->nullable();
            $table->timestamps();
            
            $table->unique(['user_id', 'category_id', 'year', 'month'], 'unique_budget_category_period');
            $table->index(['user_id', 'year', 'month']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('budgets');
    }
};
