<?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::table('sy_loans', function (Blueprint $blueprint) {
            $blueprint->unsignedBigInteger('fk_loan_renewed')->nullable()->after('visible');
            $blueprint->boolean('hasspecial')->default(false)->after('fk_status');

            $blueprint->foreign('fk_loan_renewed')->references('id')->on('sy_loans');
        });

        DB::table('sy_loans')->whereNotNull('fk_collaborator')->update(['hasspecial' => true]);
    }

    public function down(): void
    {
        Schema::table('sy_loans', function (Blueprint $blueprint) {
            $blueprint->dropForeign(['fk_loan_renewed']);
            $blueprint->dropColumn(['fk_loan_renewed', 'hasspecial']);
        });
    }
};
