<?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 ACTIVA'],
            ['is_active', true]
        ])->first();

        Schema::create('sy_loans', 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->unsignedBigInteger('fk_promoter');
            $blueprint->unsignedBigInteger('fk_client');
            $blueprint->integer('duration')->nullable();
            $blueprint->date('loandate');
            $blueprint->date('loanenddate');
            $blueprint->double('loanamount');
            $blueprint->double('weeklypayment');
            $blueprint->double('realamount');
            $blueprint->unsignedBigInteger('fk_executive');
            $blueprint->unsignedBigInteger('fk_supervisor');
            $blueprint->unsignedBigInteger('fk_collaborator')->nullable();
            $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_promoter')->references('id')->on('ct_collaborators');
            $blueprint->foreign('fk_client')->references('id')->on('sy_clients');
            $blueprint->foreign('fk_executive')->references('id')->on('ct_collaborators');
            $blueprint->foreign('fk_supervisor')->references('id')->on('ct_collaborators');
            $blueprint->foreign('fk_collaborator')->references('id')->on('ct_collaborators');
            $blueprint->foreign('fk_status')->references('id')->on('sy_statuses');
            $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_loans');
    }
};
