<?php

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

class AddFieldsToBankAccountsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('bank_accounts', function (Blueprint $table) {
            $table->string('account_name')->nullable()->after('finance_group_id');
            $table->string('account_holder')->nullable()->after('account_name');
            $table->string('branch_name')->nullable()->after('account_number');
            $table->decimal('daily_limit', 15, 2)->nullable()->after('branch_code');
            $table->unsignedBigInteger('created_by')->nullable()->after('daily_limit');
            $table->index('created_by');
        });

        DB::table('bank_accounts')
            ->whereNull('account_name')
            ->update([
                'account_name' => DB::raw('account_holder_name'),
                'account_holder' => DB::raw('account_holder_name'),
                'branch_name' => DB::raw('branch_code'),
            ]);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('bank_accounts', function (Blueprint $table) {
            $table->dropIndex(['created_by']);
            $table->dropColumn(['account_name', 'account_holder', 'branch_name', 'daily_limit', 'created_by']);
        });
    }
}