<?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
    {
        // PRC-555 — SEO metadata kolonlar + homepage NULL slug için kısıt gevşetme.
        Schema::table('route_slugs', function (Blueprint $table) {
            $table->string('meta_title', 255)->nullable()->after('slug');
            $table->text('meta_description')->nullable()->after('meta_title');
            $table->string('og_image', 255)->nullable()->after('meta_description');
        });

        // slug UNIQUE constraint'i kaldır + nullable yap (homepage için NULL slug).
        // Application-level validation duplicate kontrolü zaten yapıyor.
        // doctrine/dbal yoksa raw SQL.
        try {
            Schema::table('route_slugs', function (Blueprint $table) {
                $table->dropUnique('route_slugs_slug_unique');
            });
        } catch (\Throwable $e) {
            // Index zaten yok olabilir (fresh install farklı sırayla migration).
        }

        // Slug nullable
        DB::statement('ALTER TABLE route_slugs MODIFY COLUMN slug VARCHAR(128) NULL');
    }

    public function down(): void
    {
        Schema::table('route_slugs', function (Blueprint $table) {
            $table->dropColumn(['meta_title', 'meta_description', 'og_image']);
        });
        DB::statement('ALTER TABLE route_slugs MODIFY COLUMN slug VARCHAR(128) NOT NULL');
        Schema::table('route_slugs', function (Blueprint $table) {
            $table->unique('slug');
        });
    }
};
