<?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('campaign_events', function (Blueprint $table) {
            $table->id();
            $table->foreignId('campaign_id')->constrained()->cascadeOnDelete();
            $table->foreignId('contact_id')->constrained()->cascadeOnDelete();
            $table->string('node_id', 64)->nullable();
            $table->string('event_type', 32); // open, click, unsubscribe, conversion
            $table->string('url', 2048)->nullable();
            $table->string('ip', 45)->nullable();
            $table->string('user_agent', 512)->nullable();
            $table->timestamps();

            $table->index(['campaign_id', 'event_type']);
            $table->index(['campaign_id', 'contact_id', 'event_type']);
        });
    }

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