<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('mailings', function (Blueprint $table) {
            $table->id();
            $table->string('title', 255);
            $table->enum('type', ['local_newsletter', 'general_newsletter', 'system_message']);
            $table->string('subject', 255);
            $table->json('content_blocks')->nullable(); // Array of selected post IDs and their order
            $table->datetime('scheduled_at')->nullable();
            $table->datetime('sent_at')->nullable();
            $table->enum('status', ['draft', 'scheduled', 'sending', 'sent', 'cancelled'])->default('draft');
            $table->integer('recipients_count')->default(0);
            $table->integer('sent_count')->default(0);
            $table->integer('failed_count')->default(0);
            $table->integer('bounced_count')->default(0);

            // Polymorphic relationship for created_by
            $table->unsignedBigInteger('created_by_id');
            $table->string('created_by_type');
            $table->index(['created_by_id', 'created_by_type']);

            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('mailings');
    }
};