<?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('voucher_number')->unique();
            $table->date('expense_date');
            $table->foreignId('expense_day_id')->constrained('expense_days')->cascadeOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained('branches')->nullOnDelete();
            $table->foreignId('expense_category_id')->constrained('expense_categories')->restrictOnDelete();
            $table->decimal('amount_original', 15, 2);
            $table->string('currency', 3)->default('AFN');
            $table->decimal('exchange_rate', 15, 6)->nullable();
            $table->decimal('amount_usd', 15, 2)->nullable();
            $table->string('payment_method', 20);
            $table->string('vendor_paid_to')->nullable();
            $table->string('reference_number')->nullable();
            $table->text('description')->nullable();
            $table->string('status', 20)->default('paid');
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['expense_date', 'status']);
            $table->index(['branch_id', 'expense_date']);
        });
    }

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