<?php
// database/migrations/2026_05_28_000002_create_expenses_table.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('expenses', function (Blueprint $table) {
            $table->id();
            $table->string('description', 255);
            $table->decimal('amount', 12, 2);
            $table->decimal('aux_amount', 12, 2)->nullable()->comment('Monto en moneda base');
            $table->decimal('amount_rate', 10, 4)->nullable()->comment('Tasa de cambio usada');
            $table->date('expense_date');
            $table->enum('payment_method', ['pago movil','efectivo', 'transferencia', 'tarjeta', 'cheque'])->default('efectivo');
            $table->string('reference', 100)->nullable();
            $table->text('notes')->nullable();
            $table->string('receipt_url', 100)->nullable();
            $table->enum('status', ['pendiente', 'pagado', 'cancelado'])->default('pagado');
            
            // Relaciones
            $table->unsignedBigInteger('category_id');
            $table->unsignedBigInteger('user_id')->comment('Usuario que registró el gasto');
            $table->unsignedBigInteger('company_id');
            $table->unsignedBigInteger('supplier_id')->nullable();
            
            $table->timestamps();
            
            // Foreign keys
            $table->foreign('category_id')->references('id')->on('expense_categories')->onDelete('restrict');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('restrict');
            $table->foreign('company_id')->references('id')->on('company')->onDelete('cascade');
            $table->foreign('supplier_id')->references('id')->on('supplier')->onDelete('set null');
            
            // Índices
            $table->index('expense_date');
            $table->index('status');
            $table->index('category_id');
            $table->index('company_id');
        });
    }

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