<?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('bulk_email_campaigns', function (Blueprint $t) {
            $t->id();
            $t->foreignId('solicitor_id')->constrained()->cascadeOnDelete();
            $t->foreignId('template_id')->constrained('contact_templates');
            $t->foreignId('sender_email_id')->nullable()
                ->constrained('solicitor_email_senders')->nullOnDelete();
            $t->json('filters')->nullable();
            $t->string('status', 20)->default('pending');

            $t->unsignedInteger('total_recipients')->default(0);
            $t->unsignedInteger('queued_count')->default(0);
            $t->unsignedInteger('suppressed_count')->default(0);
            $t->unsignedInteger('deduped_count')->default(0);
            $t->unsignedInteger('rate_limited_count')->default(0);
            $t->unsignedInteger('failed_count')->default(0);
            $t->unsignedInteger('processed_count')->default(0);

            $t->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $t->timestamp('started_at')->nullable();
            $t->timestamp('completed_at')->nullable();
            $t->text('last_error')->nullable();
            $t->timestamps();

            $t->index(['solicitor_id', 'status']);
        });
    }

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