<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * What the user wants to advertise, before any platform is chosen.
 *
 * Every field a campaign needs currently lives on `campaigns`, and `campaigns`
 * has a `platform` column. That is exactly why advertising the same thing on a
 * second platform would ask for all of it again: there is nowhere for an answer
 * that is true regardless of platform to live.
 *
 * So the shared answers move here, one row per conversation, and each platform's
 * campaign points at it. Nothing reads this yet; that is the next step.
 */
return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('briefs')) {
            Schema::create('briefs', function (Blueprint $table) {
                $table->id();
                $table->foreignId('user_id')->constrained()->cascadeOnDelete();

                // Matches agent_conversations.id, which is a uuid string rather
                // than an integer. Not a foreign key: that table belongs to the
                // laravel/ai package and its shape is not ours to depend on.
                $table->string('conversation_id', 36)->nullable()->index();

                // The subject of the whole exercise, and the first thing asked.
                $table->string('landing_url')->nullable();
                $table->text('promoting')->nullable();

                // What the buyer wants, in plain words. Each platform maps this
                // to its own vocabulary: Meta's OUTCOME_SALES, Google's
                // conversion goal. Stored as the plain answer, never as one
                // platform's enum.
                $table->string('outcome')->nullable();

                $table->json('locations')->nullable();

                // The money before it is split across platforms. Each campaign
                // keeps its own share, so every existing budget check still
                // reads the campaign and needs no change.
                $table->decimal('budget', 12, 2)->nullable();
                $table->string('budget_mode')->nullable();
                $table->string('currency', 3)->nullable();

                $table->timestamp('starts_at')->nullable();
                $table->timestamp('ends_at')->nullable();

                // What the landing page said, kept so the flow can suggest from
                // it repeatedly without fetching the page on every turn.
                $table->json('page_analysis')->nullable();

                $table->timestamps();
            });
        }

        if (! Schema::hasColumn('campaigns', 'brief_id')) {
            Schema::table('campaigns', function (Blueprint $table) {
                // Nullable on purpose, and it stays that way: an adopted
                // campaign is one already running on the platform, pulled in so
                // it can be edited here. It arrives with its own values and no
                // brief, and editing it has to work exactly like that.
                $table->foreignId('brief_id')->nullable()->after('user_id')
                    ->constrained()->nullOnDelete();
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasColumn('campaigns', 'brief_id')) {
            Schema::table('campaigns', function (Blueprint $table) {
                $table->dropConstrainedForeignId('brief_id');
            });
        }

        // Dropped after the column that references it, or the constraint blocks it.
        Schema::dropIfExists('briefs');
    }
};
