<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('stories')) {
            return;
        }

        if (DB::getDriverName() === 'mysql' && Schema::hasColumn('stories', 'status')) {
            DB::statement("ALTER TABLE stories MODIFY status ENUM('draft','pending_review','published','scheduled','archived','rejected') NOT NULL DEFAULT 'draft'");
        }

        Schema::table('stories', function (Blueprint $table): void {
            if (! Schema::hasColumn('stories', 'scheduled_for')) {
                $table->timestamp('scheduled_for')->nullable()->after('published_at');
            }

            if (! Schema::hasColumn('stories', 'meta_title')) {
                $table->string('meta_title', 255)->nullable()->after('reading_time');
            }

            if (! Schema::hasColumn('stories', 'meta_description')) {
                $table->string('meta_description', 300)->nullable()->after('meta_title');
            }

            if (! Schema::hasColumn('stories', 'canonical_url')) {
                $table->string('canonical_url', 500)->nullable()->after('meta_description');
            }

            if (! Schema::hasColumn('stories', 'og_image')) {
                $table->string('og_image', 500)->nullable()->after('canonical_url');
            }

            if (! Schema::hasColumn('stories', 'submitted_for_review_at')) {
                $table->timestamp('submitted_for_review_at')->nullable()->after('scheduled_for');
            }

            if (! Schema::hasColumn('stories', 'reviewed_at')) {
                $table->timestamp('reviewed_at')->nullable()->after('submitted_for_review_at');
            }

            if (! Schema::hasColumn('stories', 'reviewed_by_id')) {
                $table->foreignId('reviewed_by_id')->nullable()->after('reviewed_at')->constrained('users')->nullOnDelete();
            }

            if (! Schema::hasColumn('stories', 'rejected_reason')) {
                $table->text('rejected_reason')->nullable()->after('reviewed_by_id');
            }

            $table->index(['status', 'submitted_for_review_at'], 'idx_stories_review_queue');
            $table->index(['creator_id', 'status', 'updated_at'], 'idx_stories_creator_status_updated');
        });
    }

    public function down(): void
    {
        if (! Schema::hasTable('stories')) {
            return;
        }

        Schema::table('stories', function (Blueprint $table): void {
            if (Schema::hasColumn('stories', 'reviewed_by_id')) {
                $table->dropConstrainedForeignId('reviewed_by_id');
            }

            foreach ([
                'scheduled_for',
                'meta_title',
                'meta_description',
                'canonical_url',
                'og_image',
                'submitted_for_review_at',
                'reviewed_at',
                'rejected_reason',
            ] as $column) {
                if (Schema::hasColumn('stories', $column)) {
                    $table->dropColumn($column);
                }
            }

            $table->dropIndex('idx_stories_review_queue');
            $table->dropIndex('idx_stories_creator_status_updated');
        });

        if (DB::getDriverName() === 'mysql' && Schema::hasColumn('stories', 'status')) {
            DB::statement("ALTER TABLE stories MODIFY status ENUM('draft','published','scheduled','archived') NOT NULL DEFAULT 'draft'");
        }
    }
};
