<?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('campaigns', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('parent_id')->nullable();
            $table->string('type', 50)->default('campaign');
            $table->string('title', 192);
            $table->string('slug', 192)->unique();
            $table->text('available_urls')->nullable();
            $table->string('status', 50)->default('draft'); // draft, scheduled, sent, paused
            $table->unsignedBigInteger('template_id')->nullable();
            $table->unsignedBigInteger('sender_profile_id')->nullable();
            $table->string('email_subject', 192)->nullable();
            $table->string('email_pre_header', 192)->nullable();
            $table->longText('email_body')->nullable();
            $table->integer('recipients_count')->default(0);
            $table->longText('recipients_config')->nullable(); // JSON: {included:[{list_id,tag_id}], excluded:[...]}
            $table->integer('delay')->nullable()->default(0);
            $table->tinyInteger('utm_status')->nullable()->default(0);
            $table->string('utm_source', 192)->nullable();
            $table->string('utm_medium', 192)->nullable();
            $table->string('utm_campaign', 192)->nullable();
            $table->string('utm_term', 192)->nullable();
            $table->string('utm_content', 192)->nullable();
            $table->string('design_template', 192)->nullable();
            $table->timestamp('scheduled_at')->nullable();
            $table->longText('settings')->nullable(); // JSON: {blocks:[...], ab_test, custom_from, ...}
            $table->unsignedBigInteger('created_by')->nullable();
            $table->timestamps();

            $table->foreign('sender_profile_id')
                ->references('id')->on('sender_profiles')
                ->nullOnDelete();
        });
    }

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