<?php

use App\Models\Application\Status;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        $loan_status = Status::where([
            ['statusname', 'SOLICITUD EN APROBACIÓN'],
            ['is_active', true]
        ])->first();

        Schema::create('sy_authorizations', function (Blueprint $blueprint) use ($loan_status) {
            $blueprint->id();
            $blueprint->unsignedBigInteger('fk_site');
            $blueprint->unsignedBigInteger('fk_office');
            $blueprint->unsignedBigInteger('fk_loan_type');
            $blueprint->unsignedBigInteger('fk_group');
            $blueprint->string('fullname', 50);
            $blueprint->date('loandate');
            $blueprint->double('loanamount');
            $blueprint->double('weeklypayment');
            $blueprint->double('renewpayment')->default(0);
            $blueprint->double('paperworkpayment')->default(0);
            $blueprint->double('otherpayment')->default(0);
            $blueprint->double('realamount');
            $blueprint->unsignedBigInteger('fk_status')->default($loan_status->id);
            $blueprint->boolean('visible')->default(true);
            $blueprint->boolean('available')->default(true);
            $blueprint->unsignedBigInteger('fk_usercreator')->nullable();
            $blueprint->timestamp('created_at')->useCurrent();
            $blueprint->unsignedBigInteger('fk_userupdater')->nullable();
            $blueprint->timestamp('updated_at')->useCurrentOnUpdate()->nullable();
            $blueprint->boolean('is_active')->default(true);

            $blueprint->foreign('fk_site')->references('id')->on('sy_sites');
            $blueprint->foreign('fk_office')->references('id')->on('sy_offices');
            $blueprint->foreign('fk_loan_type')->references('id')->on('sy_loan_types');
            $blueprint->foreign('fk_group')->references('id')->on('sy_groups');
            $blueprint->foreign('fk_usercreator')->references('id')->on('sc_users');
            $blueprint->foreign('fk_userupdater')->references('id')->on('sc_users');
        });
    }

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