<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('sy_loan_types', function (Blueprint $blueprint) {
            $blueprint->id();
            $blueprint->unsignedBigInteger('fk_site');
            $blueprint->unsignedBigInteger('fk_office');
            $blueprint->string('loantypename', 25);
            $blueprint->integer('period');
            $blueprint->integer('duration')->nullable();
            $blueprint->double('weeklypayment')->nullable();
            $blueprint->double('interesrate')->nullable();
            $blueprint->string('alloweddays', 25);
            $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_usercreator')->references('id')->on('sc_users');
            $blueprint->foreign('fk_userupdater')->references('id')->on('sc_users');
        });

        DB::table('sy_loan_types')->insert([
            [
                'fk_site' => 1,
                'fk_office' => 1,
                'loantypename' => '14 SEMANAS',
                'period' => 7,
                'duration' => 14,
                'weeklypayment' => 100,
                'alloweddays' => 'SATURDAY',
                'fk_usercreator' => 1
            ]
        ]);
    }

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