<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('sy_statuses', function (Blueprint $blueprint) {
            $blueprint->id();
            $blueprint->unsignedBigInteger('fk_site')->nullable();
            $blueprint->unsignedBigInteger('fk_office')->nullable();
            $blueprint->unsignedBigInteger('fk_status_type');
            $blueprint->string('statusname', 35);
            $blueprint->string('classname', 20)->default('danger');
            $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_status_type')->references('id')->on('ct_status_types');
            $blueprint->foreign('fk_usercreator')->references('id')->on('sc_users');
            $blueprint->foreign('fk_userupdater')->references('id')->on('sc_users');
        });

        $statuses = [];

        $client_statuses = [
            ['fk_status_type' => 1, 'statusname' => 'CLIENTE NUEVO', 'classname' => 'primary', 'fk_usercreator' => 1],
            ['fk_status_type' => 1, 'statusname' => 'CLIENTE ACTIVO', 'classname' => 'success', 'fk_usercreator' => 1],
            ['fk_status_type' => 1, 'statusname' => 'CLIENTE INACTIVO', 'classname' => 'secondary', 'fk_usercreator' => 1],
            ['fk_status_type' => 1, 'statusname' => 'CLIENTE MOROSO', 'classname' => 'danger', 'fk_usercreator' => 1],
            ['fk_status_type' => 1, 'statusname' => 'CLIENTE IMPUNTUAL', 'classname' => 'warning', 'fk_usercreator' => 1]
        ];

        $endorsement_statuses = [
            ['fk_status_type' => 2, 'statusname' => 'AVAL ACTIVO', 'classname' => 'success', 'fk_usercreator' => 1],
            ['fk_status_type' => 2, 'statusname' => 'AVAL INACTIVO', 'classname' => 'secondary', 'fk_usercreator' => 1],
            ['fk_status_type' => 2, 'statusname' => 'AVAL MOROSO', 'classname' => 'danger', 'fk_usercreator' => 1],
            ['fk_status_type' => 2, 'statusname' => 'AVAL IMPUNTUAL', 'classname' => 'warning', 'fk_usercreator' => 1]
        ];

        $loan_statuses = [
            ['fk_status_type' => 3, 'statusname' => 'SOLICITUD ACTIVA', 'classname' => 'success', 'fk_usercreator' => 1],
            ['fk_status_type' => 3, 'statusname' => 'SOLICITUD ATRASADA', 'classname' => 'warning', 'fk_usercreator' => 1],
            ['fk_status_type' => 3, 'statusname' => 'SOLICITUD FINALIZADA', 'classname' => 'secondary', 'fk_usercreator' => 1],
            ['fk_status_type' => 3, 'statusname' => 'SOLICITUD RENOVADA', 'classname' => 'info', 'fk_usercreator' => 1],
            ['fk_status_type' => 3, 'statusname' => 'SOLICITUD MOROSA', 'classname' => 'danger', 'fk_usercreator' => 1],
            ['fk_status_type' => 3, 'statusname' => 'SOLICITUD REESTRUCTURADA', 'classname' => 'dark', 'fk_usercreator' => 1],
            ['fk_status_type' => 3, 'statusname' => 'SOLICITUD EN APROBACIÓN', 'classname' => 'secondary', 'fk_usercreator' => 1],
            ['fk_status_type' => 3, 'statusname' => 'SOLICITUD APROBADA', 'classname' => 'success', 'fk_usercreator' => 1],
            ['fk_status_type' => 3, 'statusname' => 'SOLICITUD RECHAZADA', 'classname' => 'danger', 'fk_usercreator' => 1],
        ];

        $payment_statuses = [
            ['fk_status_type' => 4, 'statusname' => 'PENDIENTE', 'classname' => 'secondary', 'fk_usercreator' => 1],
            ['fk_status_type' => 4, 'statusname' => 'PUNTUAL', 'classname' => 'success', 'fk_usercreator' => 1],
            ['fk_status_type' => 4, 'statusname' => 'RECUPERADO', 'classname' => 'dark', 'fk_usercreator' => 1],
            ['fk_status_type' => 4, 'statusname' => 'IMPUNTUAL', 'classname' => 'warning', 'fk_usercreator' => 1],
            ['fk_status_type' => 4, 'statusname' => 'FALLA', 'classname' => 'danger', 'fk_usercreator' => 1],
            ['fk_status_type' => 4, 'statusname' => 'INCOMPLETO', 'classname' => 'info', 'fk_usercreator' => 1],
            ['fk_status_type' => 4, 'statusname' => 'ADELANTADO', 'classname' => 'primary', 'fk_usercreator' => 1],
            ['fk_status_type' => 4, 'statusname' => 'ADELANTADO POR RENOVACION', 'classname' => 'primary', 'fk_usercreator' => 1],
            ['fk_status_type' => 4, 'statusname' => 'REESTRUCTURADO', 'classname' => 'secondary', 'fk_usercreator' => 1],
        ];

        $statuses[] = $client_statuses;
        $statuses[] = $endorsement_statuses;
        $statuses[] = $loan_statuses;
        $statuses[] = $payment_statuses;

        foreach ($statuses as $status) {
            DB::table('sy_statuses')->insert($status);
        }
    }

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