<?php

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

class CreateBankAccountsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('bank_accounts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('finance_user_id');
            $table->unsignedBigInteger('finance_group_id');
            $table->string('account_holder_name');
            $table->string('bank_name');
            $table->string('iban');
            $table->string('account_number')->nullable();
            $table->string('branch_code')->nullable();
            $table->decimal('min_amount', 15, 2)->default(0);
            $table->decimal('max_amount', 15, 2)->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->foreign('finance_user_id')->references('id')->on('finance_users')->onDelete('cascade');
            $table->foreign('finance_group_id')->references('id')->on('finance_groups')->onDelete('cascade');
            $table->index('is_active');
            $table->index(['min_amount', 'max_amount']);
        });
    }

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