<?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
    {
        $default_status = Status::where([
            ['statusname', 'PENDIENTE']
        ])->first();

        Schema::create('sy_payments', function (Blueprint $blueprint) use ($default_status) {
            $blueprint->id();
            $blueprint->unsignedBigInteger('fk_loan');
            $blueprint->integer('paymentnumber');
            $blueprint->date('duedate');
            $blueprint->double('payment')->nullable();
            $blueprint->date('paymentdate')->nullable();
            $blueprint->unsignedBigInteger('fk_status')->default($default_status->id);
            $blueprint->boolean('available')->default(true);
            $blueprint->unsignedBigInteger('fk_usercreator');
            $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_loan')->references('id')->on('sy_loans');
            $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');

            $blueprint->index('fk_loan');
            $blueprint->index('fk_status');
            $blueprint->index('duedate');
            $blueprint->index('paymentdate');
            $blueprint->index('paymentnumber');
        });
    }

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