<?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('email_marketing_lists', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('description')->nullable();
            $table->string('provider')->nullable(); // mailchimp, mailerlite, local
            $table->string('remote_id')->nullable();
            $table->boolean('status')->default(true);
            $table->unsignedInteger('subscribers_count')->default(0);
            $table->string('default_from_name')->nullable();
            $table->string('default_from_email')->nullable();
            $table->string('default_reply_to')->nullable();
            $table->timestamps();
        });

        Schema::create('email_campaigns', function (Blueprint $table) {
            $table->id();
            $table->foreignId('email_marketing_list_id')->nullable()->constrained('email_marketing_lists')->nullOnDelete();
            $table->string('name');
            $table->string('subject')->nullable();
            $table->string('from_name')->nullable();
            $table->string('from_email')->nullable();
            $table->string('reply_to')->nullable();
            $table->string('provider')->nullable();
            $table->string('remote_id')->nullable();
            $table->enum('status', ['draft', 'scheduled', 'sending', 'sent', 'paused', 'failed'])->default('draft');
            $table->timestamp('scheduled_at')->nullable();
            $table->timestamp('sent_at')->nullable();
            $table->enum('audience_type', ['all', 'segment', 'filter'])->default('all');
            $table->json('audience_filter')->nullable();
            $table->enum('content_type', ['template', 'html'])->default('template');
            $table->foreignId('template_id')->nullable()->constrained('communication_templates')->nullOnDelete();
            $table->longText('content_html')->nullable();
            $table->unsignedInteger('queued_count')->default(0);
            $table->unsignedInteger('sent_count')->default(0);
            $table->unsignedInteger('open_count')->default(0);
            $table->unsignedInteger('click_count')->default(0);
            $table->unsignedInteger('bounce_count')->default(0);
            $table->unsignedInteger('unsubscribe_count')->default(0);
            $table->timestamps();
        });

        Schema::create('email_marketing_list_subscribers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('email_marketing_list_id')->constrained('email_marketing_lists')->cascadeOnDelete();
            $table->foreignId('client_id')->nullable()->constrained('clients')->nullOnDelete();
            $table->string('email');
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->boolean('status')->default(true); // subscribed
            $table->string('provider')->nullable();
            $table->string('remote_id')->nullable();
            $table->timestamp('subscribed_at')->nullable();
            $table->timestamp('unsubscribed_at')->nullable();
            $table->timestamps();
            // Manuel kısa index adı vererek MySQL 64 char limitini aşmayalım
            $table->unique(['email', 'email_marketing_list_id'], 'eml_sub_email_list_uq');
        });
    }

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