<?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_performance_suggestions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('platform', 24);
            $table->string('external_campaign_id');
            $table->foreignId('local_campaign_id')->nullable()
                ->constrained('campaigns')->nullOnDelete();
            $table->unsignedBigInteger('local_google_ads_campaign_id')->nullable();

            $table->string('status', 32);
            $table->string('source', 16);
            $table->text('reason');
            $table->string('confidence', 16)->nullable();
            $table->string('action_state', 16)->default('pending');

            $table->json('snapshot')->nullable();
            $table->string('model')->nullable();
            $table->text('last_error')->nullable();
            $table->timestamp('suggested_at')->nullable();
            $table->timestamps();

            $table->unique(
                ['platform', 'external_campaign_id'],
                'campaign_perf_suggestions_platform_external_unique',
            );
            $table->index(['user_id', 'status']);
        });
    }

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