<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('expenses', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('academy_id')->constrained('academies');
            $table->foreignId('branch_id')->nullable()->constrained('branches');
            $table->string('category', 30);
            $table->bigInteger('amount');
            $table->string('description', 500);
            $table->string('recipient_name', 255)->nullable();
            $table->string('payment_method', 30)->default('cash');
            $table->string('receipt_reference', 100)->nullable();
            $table->date('expense_date');
            $table->text('notes')->nullable();
            $table->foreignId('created_by')->constrained('users');
            $table->timestamps();
            $table->softDeletes();

            $table->index(['academy_id', 'category', 'expense_date']);
            $table->index(['academy_id', 'expense_date']);
        });

        DB::statement("ALTER TABLE expenses ADD CONSTRAINT expenses_category_check CHECK (category IN ('maintenance', 'supplies', 'transport', 'food_beverage', 'sports_equipment', 'printing', 'cleaning', 'medical', 'utilities', 'other'))");
        DB::statement("ALTER TABLE expenses ADD CONSTRAINT expenses_payment_method_check CHECK (payment_method IN ('cash', 'card', 'bank_transfer', 'wallet', 'online', 'cheque', 'other'))");
    }

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