<?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
    {
        // ── Newsletter Subscribers ───────────────────────────────────────────
        Schema::create('subscribers', function (Blueprint $table) {
            $table->id();
            $table->string('email', 150)->unique();
            $table->string('name', 100)->nullable();
            $table->enum('status', ['pending', 'confirmed', 'unsubscribed'])->default('pending');
            $table->string('confirmation_token', 100)->nullable();
            $table->timestamp('confirmed_at')->nullable();
            $table->timestamp('unsubscribed_at')->nullable();
            $table->enum('source', ['homepage', 'post', 'popup', 'api'])->default('homepage');
            $table->string('ip_address', 45)->nullable();
            $table->timestamps();

            $table->index(['status', 'email']);
        });

        // ── Newsletter Campaigns ─────────────────────────────────────────────
        Schema::create('newsletter_campaigns', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title', 200);
            $table->string('subject', 200);
            $table->longText('content');
            $table->enum('status', ['draft', 'scheduled', 'sending', 'sent'])->default('draft');
            $table->unsignedInteger('total_recipients')->default(0);
            $table->unsignedInteger('sent_count')->default(0);
            $table->unsignedInteger('open_count')->default(0);
            $table->unsignedInteger('click_count')->default(0);
            $table->timestamp('scheduled_at')->nullable();
            $table->timestamp('sent_at')->nullable();
            $table->foreignId('created_by')->constrained('users');
            $table->timestamps();
        });

        // ── Contact Messages ─────────────────────────────────────────────────
        Schema::create('contact_messages', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 100);
            $table->string('email', 150);
            $table->string('subject', 200)->nullable();
            $table->text('message');
            $table->enum('type', ['general', 'advertise', 'partnership', 'report'])->default('general');
            $table->enum('status', ['unread', 'read', 'replied', 'archived'])->default('unread');
            $table->string('ip_address', 45)->nullable();
            $table->timestamp('replied_at')->nullable();
            $table->foreignId('replied_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(['status', 'type']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('contact_messages');
        Schema::dropIfExists('newsletter_campaigns');
        Schema::dropIfExists('subscribers');
    }
};
