<?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
    {
        // FAQ categories
        Schema::table('website_faqs', function (Blueprint $table) {
            $table->string('category', 100)->nullable()->after('answer_en');
            $table->string('category_en', 100)->nullable()->after('category');
        });

        // News categories
        Schema::table('website_news', function (Blueprint $table) {
            $table->string('category', 100)->nullable()->after('excerpt_en');
            $table->string('category_en', 100)->nullable()->after('category');
        });

        // Testimonials video support
        Schema::table('website_testimonials', function (Blueprint $table) {
            $table->string('video_url', 500)->nullable()->after('content_en');
        });

        // Gallery category on media
        if (!Schema::hasColumn('media', 'gallery_category')) {
            Schema::table('media', function (Blueprint $table) {
                $table->string('gallery_category', 50)->nullable()->after('alt_text_en');
            });
        }

        // Floating elements, announcement bar, popup config
        Schema::table('website_settings', function (Blueprint $table) {
            $table->jsonb('floating_elements')->default('{}')->after('footer_style');
            $table->jsonb('announcement_bar')->default('{}')->after('floating_elements');
            $table->jsonb('popup_config')->default('{}')->after('announcement_bar');
        });

        // Update section_key CHECK to include video + custom_html
        DB::statement("ALTER TABLE website_sections DROP CONSTRAINT IF EXISTS website_sections_section_key_check");
        DB::statement("ALTER TABLE website_sections ADD CONSTRAINT website_sections_section_key_check CHECK (section_key IN (
            'hero', 'about', 'activities', 'programs', 'branches', 'schedule',
            'trainers', 'gallery', 'testimonials', 'pricing', 'partners',
            'contact', 'faq', 'cta', 'stats', 'news', 'video', 'custom_html'
        ))");
    }

    public function down(): void
    {
        Schema::table('website_faqs', function (Blueprint $table) {
            $table->dropColumn(['category', 'category_en']);
        });

        Schema::table('website_news', function (Blueprint $table) {
            $table->dropColumn(['category', 'category_en']);
        });

        Schema::table('website_testimonials', function (Blueprint $table) {
            $table->dropColumn('video_url');
        });

        if (Schema::hasColumn('media', 'gallery_category')) {
            Schema::table('media', function (Blueprint $table) {
                $table->dropColumn('gallery_category');
            });
        }

        Schema::table('website_settings', function (Blueprint $table) {
            $table->dropColumn(['floating_elements', 'announcement_bar', 'popup_config']);
        });

        DB::statement("ALTER TABLE website_sections DROP CONSTRAINT IF EXISTS website_sections_section_key_check");
        DB::statement("ALTER TABLE website_sections ADD CONSTRAINT website_sections_section_key_check CHECK (section_key IN (
            'hero', 'about', 'activities', 'programs', 'branches', 'schedule',
            'trainers', 'gallery', 'testimonials', 'pricing', 'partners',
            'contact', 'faq', 'cta', 'stats', 'news'
        ))");
    }
};
