<?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
    {
        if (!Schema::hasTable('finance_follow_up_notes')) {
            Schema::create('finance_follow_up_notes', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('parent_id')->index();
                $table->unsignedBigInteger('invoice_id')->nullable()->index();
                $table->string('school_year')->nullable()->index();
                $table->string('status')->default('not_contacted')->index();
                $table->string('contact_method')->nullable();
                $table->date('promise_to_pay_date')->nullable();
                $table->decimal('promise_to_pay_amount', 12, 2)->nullable();
                $table->date('next_follow_up_date')->nullable()->index();
                $table->unsignedTinyInteger('escalation_level')->default(0);
                $table->text('note')->nullable();
                $table->unsignedBigInteger('created_by')->nullable()->index();
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('finance_balance_carryforwards')) {
            Schema::create('finance_balance_carryforwards', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('parent_id')->index();
                $table->unsignedBigInteger('student_id')->nullable()->index();
                $table->string('from_school_year')->index();
                $table->string('to_school_year')->index();
                $table->json('source_invoice_ids_json')->nullable();
                $table->decimal('source_balance_amount', 12, 2)->default(0);
                $table->decimal('carryforward_amount', 12, 2)->default(0);
                $table->decimal('waived_amount', 12, 2)->default(0);
                $table->decimal('adjusted_amount', 12, 2)->default(0);
                $table->decimal('remaining_amount', 12, 2)->default(0);
                $table->string('status')->default('draft')->index();
                $table->string('reason')->nullable();
                $table->unsignedBigInteger('created_by')->nullable()->index();
                $table->unsignedBigInteger('approved_by')->nullable()->index();
                $table->timestamp('approved_at')->nullable();
                $table->unsignedBigInteger('posted_invoice_id')->nullable()->index();
                $table->unsignedBigInteger('posted_invoice_line_item_id')->nullable()->index();
                $table->json('metadata_json')->nullable();
                $table->timestamps();
                $table->unique(['parent_id', 'from_school_year', 'to_school_year'], 'finance_carryforward_unique_parent_years');
            });
        }

        if (!Schema::hasTable('invoice_installment_plans')) {
            Schema::create('invoice_installment_plans', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('invoice_id')->index();
                $table->unsignedBigInteger('parent_id')->index();
                $table->string('school_year')->nullable()->index();
                $table->string('plan_name')->nullable();
                $table->decimal('total_amount', 12, 2)->default(0);
                $table->unsignedInteger('number_of_installments')->default(1);
                $table->string('status')->default('draft')->index();
                $table->unsignedBigInteger('created_by')->nullable()->index();
                $table->unsignedBigInteger('approved_by')->nullable()->index();
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('invoice_installments')) {
            Schema::create('invoice_installments', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('installment_plan_id')->index();
                $table->unsignedBigInteger('invoice_id')->index();
                $table->unsignedInteger('sequence');
                $table->date('due_date')->index();
                $table->decimal('amount_due', 12, 2)->default(0);
                $table->decimal('amount_paid', 12, 2)->default(0);
                $table->decimal('balance', 12, 2)->default(0);
                $table->string('status')->default('pending')->index();
                $table->timestamp('paid_at')->nullable();
                $table->timestamps();
                $table->unique(['installment_plan_id', 'sequence'], 'invoice_installments_plan_sequence_unique');
            });
        }

        if (!Schema::hasTable('payment_installment_allocations')) {
            Schema::create('payment_installment_allocations', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('payment_id')->index();
                $table->unsignedBigInteger('invoice_id')->index();
                $table->unsignedBigInteger('installment_id')->index();
                $table->decimal('amount', 12, 2)->default(0);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('finance_notification_logs')) {
            Schema::create('finance_notification_logs', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('parent_id')->nullable()->index();
                $table->unsignedBigInteger('student_id')->nullable()->index();
                $table->unsignedBigInteger('invoice_id')->nullable()->index();
                $table->unsignedBigInteger('payment_id')->nullable()->index();
                $table->unsignedBigInteger('refund_id')->nullable()->index();
                $table->unsignedBigInteger('event_charge_id')->nullable()->index();
                $table->unsignedBigInteger('installment_id')->nullable()->index();
                $table->string('notification_type')->index();
                $table->string('channel')->default('mail')->index();
                $table->string('recipient')->nullable();
                $table->string('subject')->nullable();
                $table->string('status')->default('queued')->index();
                $table->timestamp('sent_at')->nullable();
                $table->timestamp('failed_at')->nullable();
                $table->text('error_message')->nullable();
                $table->unsignedBigInteger('created_by')->nullable()->index();
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('finance_notification_preferences')) {
            Schema::create('finance_notification_preferences', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('parent_id')->unique();
                $table->boolean('email_receipts_enabled')->default(true);
                $table->boolean('email_reminders_enabled')->default(true);
                $table->boolean('sms_receipts_enabled')->default(false);
                $table->boolean('sms_reminders_enabled')->default(false);
                $table->boolean('statement_notifications_enabled')->default(true);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('finance_receipt_sequences')) {
            Schema::create('finance_receipt_sequences', function (Blueprint $table) {
                $table->id();
                $table->string('school_year')->unique();
                $table->string('prefix')->default('R');
                $table->unsignedBigInteger('next_number')->default(1);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('payment_carryforward_allocations')) {
            Schema::create('payment_carryforward_allocations', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('payment_id')->index();
                $table->unsignedBigInteger('carryforward_id')->index();
                $table->decimal('amount', 12, 2)->default(0);
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('payment_carryforward_allocations');
        Schema::dropIfExists('finance_receipt_sequences');
        Schema::dropIfExists('finance_notification_preferences');
        Schema::dropIfExists('finance_notification_logs');
        Schema::dropIfExists('payment_installment_allocations');
        Schema::dropIfExists('invoice_installments');
        Schema::dropIfExists('invoice_installment_plans');
        Schema::dropIfExists('finance_balance_carryforwards');
        Schema::dropIfExists('finance_follow_up_notes');
    }
};
