<?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('daily_branch_report_recipients', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('business_id')->constrained()->cascadeOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained()->nullOnDelete();
            $table->string('name')->nullable();
            $table->string('channel', 20);
            $table->string('destination');
            $table->boolean('is_active')->default(true);
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['business_id', 'channel', 'is_active'], 'daily_report_recipients_channel_idx');
            $table->index(['business_id', 'branch_id', 'is_active'], 'daily_report_recipients_branch_idx');
        });

        Schema::create('daily_branch_reports', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('business_id')->constrained()->cascadeOnDelete();
            $table->foreignId('branch_id')->constrained()->cascadeOnDelete();
            $table->foreignId('daily_closing_id')->unique()->constrained('daily_closings')->cascadeOnDelete();
            $table->date('report_date');
            $table->string('closing_number');
            $table->string('subject');
            $table->longText('body_html');
            $table->longText('body_text');
            $table->json('snapshot');
            $table->char('checksum', 64);
            $table->timestamp('generated_at');
            $table->timestamps();

            $table->index(['business_id', 'report_date'], 'daily_branch_reports_business_date_idx');
            $table->index(['branch_id', 'report_date'], 'daily_branch_reports_branch_date_idx');
        });

        Schema::create('daily_branch_report_deliveries', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('daily_branch_report_id')->constrained()->cascadeOnDelete();
            $table->foreignId('recipient_id')->nullable()->constrained('daily_branch_report_recipients')->nullOnDelete();
            $table->string('channel', 20);
            $table->string('recipient_name')->nullable();
            $table->string('recipient');
            $table->string('status', 20)->default('pending');
            $table->unsignedSmallInteger('attempts')->default(0);
            $table->string('provider_message_id')->nullable();
            $table->text('provider_response')->nullable();
            $table->text('error_message')->nullable();
            $table->timestamp('last_attempt_at')->nullable();
            $table->timestamp('sent_at')->nullable();
            $table->timestamps();

            $table->unique(['daily_branch_report_id', 'channel', 'recipient'], 'daily_report_delivery_recipient_unique');
            $table->index(['status', 'channel'], 'daily_report_delivery_status_idx');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('daily_branch_report_deliveries');
        Schema::dropIfExists('daily_branch_reports');
        Schema::dropIfExists('daily_branch_report_recipients');
    }
};
