<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('businesses', function (Blueprint $table): void {
            $table->boolean('payment_deadline_penalties_enabled')->default(false);
            $table->string('deadline_late_penalty_type')->default('fixed');
            $table->decimal('deadline_late_penalty_value', 18, 6)->default(0);
            $table->string('deadline_late_penalty_settlement')->default('add_to_loan');
            $table->string('missed_schedule_penalty_type')->default('fixed');
            $table->decimal('missed_schedule_penalty_value', 18, 6)->default(0);
            $table->string('missed_schedule_penalty_settlement')->default('add_to_loan');
        });

        Schema::table('loan_products', function (Blueprint $table): void {
            $table->boolean('payment_deadline_enabled')->default(false);
            $table->time('payment_deadline_time')->nullable();
        });

        Schema::table('loans', function (Blueprint $table): void {
            $table->boolean('payment_deadline_enabled')->default(false);
            $table->time('payment_deadline_time')->nullable();
            $table->string('deadline_late_penalty_type')->default('fixed');
            $table->decimal('deadline_late_penalty_value', 18, 6)->default(0);
            $table->string('deadline_late_penalty_settlement')->default('add_to_loan');
            $table->string('missed_schedule_penalty_type')->default('fixed');
            $table->decimal('missed_schedule_penalty_value', 18, 6)->default(0);
            $table->string('missed_schedule_penalty_settlement')->default('add_to_loan');
        });

        Schema::table('loan_penalty_charges', function (Blueprint $table): void {
            $table->string('penalty_reason')->default('general_overdue');
            $table->string('settlement_mode')->default('add_to_loan');
            $table->index(['loan_id', 'penalty_reason']);
        });

        Schema::create('loan_installment_excuses', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('loan_id')->constrained()->restrictOnDelete();
            $table->foreignId('loan_installment_id')->unique()->constrained('loan_installments')->restrictOnDelete();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->text('reason');
            $table->foreignId('excused_by')->nullable()->constrained('users')->nullOnDelete();
            $table->dateTime('excused_at');
            $table->timestamps();
            $table->index(['branch_id', 'excused_at']);
        });
    }

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

        Schema::table('loan_penalty_charges', function (Blueprint $table): void {
            $table->dropIndex(['loan_id', 'penalty_reason']);
            $table->dropColumn(['penalty_reason', 'settlement_mode']);
        });

        Schema::table('loans', function (Blueprint $table): void {
            $table->dropColumn([
                'payment_deadline_enabled','payment_deadline_time','deadline_late_penalty_type','deadline_late_penalty_value',
                'deadline_late_penalty_settlement','missed_schedule_penalty_type','missed_schedule_penalty_value','missed_schedule_penalty_settlement',
            ]);
        });

        Schema::table('loan_products', function (Blueprint $table): void {
            $table->dropColumn(['payment_deadline_enabled', 'payment_deadline_time']);
        });

        Schema::table('businesses', function (Blueprint $table): void {
            $table->dropColumn([
                'payment_deadline_penalties_enabled','deadline_late_penalty_type','deadline_late_penalty_value','deadline_late_penalty_settlement',
                'missed_schedule_penalty_type','missed_schedule_penalty_value','missed_schedule_penalty_settlement',
            ]);
        });
    }
};
