<?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_sync_runs', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('campaign_id')->constrained()->cascadeOnDelete();
            $table->string('platform', 20);
            $table->string('trigger', 20)->default('manual');
            $table->string('status', 20)->default('running');
            $table->boolean('complete')->default(false);
            $table->json('gaps')->nullable();
            $table->unsignedInteger('entity_count')->default(0);
            $table->unsignedInteger('drift_count')->default(0);
            $table->text('error')->nullable();
            $table->string('actor')->nullable();
            $table->timestamp('started_at');
            $table->timestamp('finished_at')->nullable();
            $table->timestamps();

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

        Schema::create('campaign_platform_entities', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('campaign_id')->constrained()->cascadeOnDelete();
            $table->string('platform', 20);
            $table->string('entity_type', 40);
            $table->string('external_id');
            $table->string('parent_type', 40)->nullable();
            $table->string('parent_external_id')->nullable();
            $table->string('name')->nullable();
            $table->string('status', 60)->nullable();
            $table->string('effective_status', 60)->nullable();
            $table->json('settings')->nullable();
            $table->string('fingerprint', 64);
            $table->timestamp('first_seen_at');
            $table->timestamp('last_seen_at');
            $table->timestamp('last_changed_at')->nullable();
            $table->timestamp('removed_at')->nullable();
            $table->timestamps();

            $table->unique(['campaign_id', 'entity_type', 'external_id'], 'campaign_platform_entities_identity');
            $table->index(['campaign_id', 'removed_at']);
        });

        Schema::create('campaign_platform_drifts', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('campaign_id')->constrained()->cascadeOnDelete();
            $table->foreignId('campaign_sync_run_id')->constrained()->cascadeOnDelete();
            $table->foreignId('campaign_platform_entity_id')->constrained()->cascadeOnDelete();
            $table->string('kind', 20);
            $table->string('field')->nullable();
            $table->json('before')->nullable();
            $table->json('after')->nullable();
            $table->timestamp('detected_at');
            $table->timestamps();

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

    public function down(): void
    {
        Schema::dropIfExists('campaign_platform_drifts');
        Schema::dropIfExists('campaign_platform_entities');
        Schema::dropIfExists('campaign_sync_runs');
    }
};
