<?php

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

/**
 * Lets a generated creative become something a campaign can publish.
 *
 * The wizard writes to creatives; ads publish from assets. Nothing joined the
 * two, so nothing the creative generator produced could ever reach an ad.
 *
 * Two columns do it. creatives.asset_id is the join, and it is also what stops
 * a second Asset being made every time a creative is touched. assets.disk is
 * needed because the two halves store files in different places: uploads land
 * on the local disk, generated creatives on the public one, and the upload to
 * Meta previously assumed local for everything.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('assets', function (Blueprint $table): void {
            $table->string('disk', 32)->default('local')->after('source');
        });

        Schema::table('creatives', function (Blueprint $table): void {
            $table->foreignId('asset_id')->nullable()->after('thumbnail_path')
                ->constrained()->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('creatives', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('asset_id');
        });

        Schema::table('assets', function (Blueprint $table): void {
            $table->dropColumn('disk');
        });
    }
};
