<?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::create('reminder_settings', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id')->nullable()->index();
            $table->boolean('enable_invoice_created')->default(true);
            $table->boolean('enable_invoice_due_reminder')->default(true);
            $table->boolean('enable_invoice_overdue_reminder')->default(true);
            $table->boolean('enable_invoice_paid')->default(true);
            $table->boolean('enable_partial_payment')->default(true);
            $table->boolean('enable_quotation_created')->default(true);
            $table->boolean('enable_quotation_expiry')->default(true);
            $table->boolean('enable_quotation_accepted')->default(true);
            $table->boolean('enable_quotation_rejected')->default(true);
            $table->boolean('enable_purchase_order_created')->default(true);
            $table->boolean('enable_recurring_invoice_generated')->default(true);
            $table->boolean('enable_payment_receipt')->default(true);
            $table->boolean('enable_manual_reminders')->default(true);
            $table->boolean('enable_customer_welcome')->default(true);
            $table->boolean('enable_follow_up_reminder')->default(true);
            $table->unsignedSmallInteger('due_reminder_days_before')->default(3);
            $table->unsignedSmallInteger('overdue_reminder_days_after')->default(1);
            $table->unsignedSmallInteger('quotation_expiry_days_before')->default(2);
            $table->boolean('send_copy_to_admin')->default(false);
            $table->string('email_sending_mode', 16)->default('queue');
            $table->boolean('attach_pdf_to_emails')->default(false);
            $table->timestamps();
        });

        Schema::create('reminder_logs', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id')->nullable()->index();
            $table->foreignId('customer_id')->nullable()->constrained('customers')->nullOnDelete();
            $table->foreignId('invoice_id')->nullable()->constrained('invoices')->nullOnDelete();
            $table->foreignId('quotation_id')->nullable()->constrained('quotations')->nullOnDelete();
            $table->foreignId('purchase_order_id')->nullable()->constrained('purchase_orders')->nullOnDelete();
            $table->foreignId('payment_id')->nullable()->constrained('payments')->nullOnDelete();
            $table->unsignedBigInteger('recurring_invoice_id')->nullable()->index();
            $table->string('reminder_type', 64)->index();
            $table->string('subject');
            $table->string('recipient_email');
            $table->string('status', 16)->default('pending')->index();
            $table->timestamp('sent_at')->nullable();
            $table->text('error_message')->nullable();
            $table->string('channel', 32)->default('email');
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(['reminder_type', 'invoice_id', 'created_at']);
            $table->index(['reminder_type', 'quotation_id', 'created_at']);
            $table->index(['recipient_email', 'reminder_type', 'created_at']);
        });
    }

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