<?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_profiles', 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('name')->nullable();
            $table->string('objective', 64)->nullable();
            $table->decimal('daily_budget', 12, 2)->nullable();
            $table->string('currency', 8)->nullable();
            $table->string('bid_strategy', 64)->nullable();

            $table->string('platform_status', 64)->nullable();
            $table->string('delivery_status', 64)->nullable();
            $table->text('delivery_detail')->nullable();

            $table->json('regions')->nullable();
            $table->json('languages')->nullable();
            $table->json('platform_learning')->nullable();
            $table->json('platform_payload')->nullable();

            $table->string('account_id', 64)->nullable();
            $table->date('started_at')->nullable();
            $table->timestamp('fetched_at')->nullable();
            $table->text('last_error')->nullable();
            $table->timestamps();

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

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