<?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('cash_movements', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('booking_id')->nullable()->constrained()->nullOnDelete();

            $table->enum('type', ['income', 'expense']);
            $table->enum('payment_method', ['cash', 'card', 'transfer', 'online', 'other'])->default('cash');
            $table->enum('status', ['confirmed', 'void'])->default('confirmed');
            $table->string('category', 80)->default('General');
            $table->string('reference', 80)->nullable();
            $table->string('description');
            $table->decimal('amount', 12, 2);
            $table->string('currency', 3)->default('COP');
            $table->dateTime('occurred_at');
            $table->text('notes')->nullable();

            $table->timestamps();
            $table->softDeletes();

            $table->index(['company_id', 'type', 'occurred_at']);
            $table->index(['company_id', 'status', 'occurred_at']);
            $table->index(['company_id', 'payment_method', 'occurred_at']);
        });
    }

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