<?php

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

/**
 * PRC-1143 — konsolide şema: `email_campaigns` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('email_campaigns', function (Blueprint $t) {
            $t->id();
            $t->unsignedBigInteger('email_marketing_list_id')->nullable();
            $t->string('name', 255);
            $t->string('subject', 255)->nullable();
            $t->string('from_name', 255)->nullable();
            $t->string('from_email', 255)->nullable();
            $t->string('reply_to', 255)->nullable();
            $t->string('provider', 255)->nullable();
            $t->string('remote_id', 255)->nullable();
            $t->enum('status', ['draft','scheduled','sending','sent','paused','failed'])->default('draft');
            $t->timestamp('scheduled_at')->nullable();
            $t->timestamp('sent_at')->nullable();
            $t->enum('audience_type', ['all','segment','filter'])->default('all');
            $t->json('audience_filter')->nullable();
            $t->enum('content_type', ['template','html'])->default('template');
            $t->unsignedBigInteger('template_id')->nullable();
            $t->longText('content_html')->nullable();
            $t->unsignedInteger('queued_count')->default(0);
            $t->unsignedInteger('sent_count')->default(0);
            $t->unsignedInteger('open_count')->default(0);
            $t->unsignedInteger('click_count')->default(0);
            $t->unsignedInteger('bounce_count')->default(0);
            $t->unsignedInteger('unsubscribe_count')->default(0);
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->index('email_marketing_list_id', 'email_campaigns_email_marketing_list_id_foreign');
            $t->index('template_id', 'email_campaigns_template_id_foreign');
            $t->foreign('email_marketing_list_id', 'email_campaigns_email_marketing_list_id_foreign')->references('id')->on('email_marketing_lists')->onDelete('set null');
            $t->foreign('template_id', 'email_campaigns_template_id_foreign')->references('id')->on('communication_templates')->onDelete('set null');
        });
    }

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