<?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('reminders_enabled')->default(true)->after('manager_approval_threshold');
            $table->json('reminder_pre_due_days')->nullable()->after('reminders_enabled');
            $table->boolean('reminder_due_date_enabled')->default(true)->after('reminder_pre_due_days');
            $table->boolean('reminder_overdue_enabled')->default(true)->after('reminder_due_date_enabled');
            $table->string('reminder_overdue_frequency', 30)->default('daily')->after('reminder_overdue_enabled');
            $table->json('reminder_channels')->nullable()->after('reminder_overdue_frequency');
        });

        Schema::create('notification_templates', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('business_id')->constrained()->restrictOnDelete();
            $table->string('event_type', 30);
            $table->string('channel', 30);
            $table->string('subject')->nullable();
            $table->text('body');
            $table->boolean('active')->default(true);
            $table->timestamps();

            $table->unique(['business_id', 'event_type', 'channel']);
            $table->index(['business_id', 'active']);
        });

        Schema::create('notification_logs', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('business_id')->constrained()->restrictOnDelete();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->foreignId('customer_id')->constrained()->restrictOnDelete();
            $table->foreignId('loan_id')->constrained()->restrictOnDelete();
            $table->foreignId('loan_installment_id')->constrained('loan_installments')->restrictOnDelete();
            $table->foreignId('notification_template_id')->nullable()->constrained('notification_templates')->nullOnDelete();
            $table->string('reminder_type', 30);
            $table->smallInteger('days_from_due');
            $table->string('channel', 30);
            $table->string('recipient');
            $table->string('subject')->nullable();
            $table->text('message');
            $table->date('scheduled_for');
            $table->string('status', 20)->default('pending');
            $table->longText('provider_response')->nullable();
            $table->text('error')->nullable();
            $table->unsignedInteger('attempts')->default(0);
            $table->timestamp('last_attempt_at')->nullable();
            $table->timestamp('delivered_at')->nullable();
            $table->string('idempotency_key', 64)->unique();
            $table->timestamps();

            $table->index(['branch_id', 'scheduled_for']);
            $table->index(['branch_id', 'status', 'scheduled_for']);
            $table->index(['loan_id', 'scheduled_for']);
            $table->index(['customer_id', 'scheduled_for']);
        });
    }

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

        Schema::table('businesses', function (Blueprint $table): void {
            $table->dropColumn([
                'reminders_enabled', 'reminder_pre_due_days', 'reminder_due_date_enabled',
                'reminder_overdue_enabled', 'reminder_overdue_frequency', 'reminder_channels',
            ]);
        });
    }
};
