<?php

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

return new class extends Migration
{
    /**
     * Tipos de conta:
     * - cash: Dinheiro em espécie
     * - checking: Conta Corrente
     * - savings: Conta Poupança
     * - credit_card: Cartão de Crédito
     * - asset: Ativos (investimentos, bens)
     * - liability: Passivos (dívidas, empréstimos)
     */
    public function up(): void
    {
        Schema::create('accounts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->string('name', 100);
            $table->enum('type', ['cash', 'checking', 'savings', 'credit_card', 'asset', 'liability']);
            $table->string('bank_name', 100)->nullable();
            $table->string('account_number', 50)->nullable();
            $table->decimal('initial_balance', 15, 2)->default(0);
            $table->decimal('current_balance', 15, 2)->default(0);
            $table->decimal('credit_limit', 15, 2)->nullable(); // Para cartões de crédito
            $table->string('currency', 3)->default('BRL');
            $table->string('color', 7)->default('#1E40AF'); // Cor para identificação visual
            $table->string('icon', 50)->default('bi-wallet2'); // Bootstrap icon
            $table->text('description')->nullable();
            $table->boolean('is_active')->default(true);
            $table->boolean('include_in_total')->default(true); // Incluir no saldo total
            $table->timestamps();
            $table->softDeletes();

            // Índices para performance
            $table->index(['user_id', 'type']);
            $table->index(['user_id', 'is_active']);
        });
    }

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