<?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_decisions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('platform', 24);
            $table->string('account_id')->nullable();
            $table->string('external_campaign_id');
            $table->string('campaign_name');
            $table->string('engine_version', 16)->default('v2');
            $table->string('trigger', 32)->default('scheduled_hourly');
            $table->timestamp('evaluated_at');
            $table->string('verdict', 32);
            $table->decimal('confidence', 4, 2)->default(0);
            $table->string('recommended_action', 32);
            $table->smallInteger('recommended_budget_change_percent')->default(0);
            $table->text('reason');
            $table->json('capabilities_json')->nullable();
            $table->json('reasons_json')->nullable();
            $table->json('metrics_json')->nullable();
            $table->json('trends_json')->nullable();
            $table->json('data_quality_json')->nullable();
            $table->json('guardrail_json')->nullable();
            $table->json('snapshot_json')->nullable();
            $table->timestamps();

            $table->index(['platform', 'external_campaign_id', 'evaluated_at'], 'campaign_decisions_lookup');
            $table->index(['verdict', 'evaluated_at']);
        });
    }

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