<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('accounts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('currency_id')->constrained()->restrictOnDelete();
            $table->string('name');
            $table->enum('type', ['cash', 'bank', 'e_wallet', 'credit_card', 'investment', 'loan', 'other']);
            $table->decimal('initial_balance', 15, 2)->default(0);
            $table->decimal('current_balance', 15, 2)->default(0);
            $table->string('icon')->nullable(); // emoji or icon class
            $table->string('color', 7)->default('#3B82F6'); // hex color
            $table->string('institution')->nullable(); // bank name, e-wallet provider
            $table->string('account_number')->nullable();
            $table->decimal('credit_limit', 15, 2)->nullable(); // for credit cards
            $table->integer('billing_day')->nullable(); // 1-31, for credit cards
            $table->text('description')->nullable();
            $table->boolean('include_in_total')->default(true);
            $table->boolean('is_active')->default(true);
            $table->integer('sort_order')->default(0);
            $table->timestamps();
            $table->softDeletes();

            $table->index(['user_id', 'type']);
            $table->index(['user_id', 'is_active']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('accounts');
    }
};
