<?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('ac_customer_balances', function (Blueprint $table) {
            $table->id();
            $table->foreignId('customer_id')->constrained()->onDelete('cascade');
            $table->decimal('current_balance', 10, 2)->default(0);
            $table->decimal('total_paid', 10, 2)->default(0);
            $table->decimal('total_refunded', 10, 2)->default(0);
            $table->decimal('total_debit', 10, 2)->default(0);
            $table->timestamp('last_transaction_date')->nullable();
            $table->enum('last_transaction_type', ['deposit', 'refund', 'expense', 'debit', 'adjustment'])->nullable();
            $table->decimal('last_transaction_amount', 10, 2)->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();

            // Indexes
            $table->unique('customer_id');
            $table->index('current_balance');
            $table->index('last_transaction_date');
        });
    }

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