<?php

declare(strict_types=1);

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('winback_recipients', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('campaign_id')->constrained('winback_campaigns')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('subscription_id')->nullable()->constrained()->cascadeOnDelete();
            $table->integer('current_email_index')->default(0);
            $table->timestamp('last_email_sent_at')->nullable();
            $table->integer('opened_count')->default(0);
            $table->integer('clicked_count')->default(0);
            $table->boolean('reactivated')->default(false);
            $table->timestamp('reactivated_at')->nullable();
            $table->timestamp('unsubscribed_at')->nullable();
            $table->timestamps();

            $table->unique(['campaign_id', 'user_id', 'subscription_id'], 'winback_recipients_unique');
        });
    }

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