<?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
    {
        if (Schema::hasTable('landing_pages') && Schema::hasColumn('landing_pages', 'website_url')) {
            Schema::rename('landing_pages', 'landing_pages_legacy');
        }

        Schema::create('landing_pages', function (Blueprint $table) {
            $table->id();
            $table->foreignId('landing_page_generation_id')->constrained()->cascadeOnDelete();
            $table->string('version');
            $table->string('title')->nullable();
            $table->string('status')->default('queued');
            $table->string('html_path')->nullable();
            $table->longText('prompt_used')->nullable();
            $table->text('failure_reason')->nullable();
            $table->timestamps();

            $table->unique(['landing_page_generation_id', 'version']);
        });
    }

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