<?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
    {
        // --- website_settings: new columns for Phase A (template, colors, typography, density, shapes) ---
        Schema::table('website_settings', function (Blueprint $table) {
            $table->string('background_color', 7)->default('#ffffff')->after('text_color');
            $table->string('surface_color', 7)->default('#f8fafc')->after('background_color');
            $table->string('border_color', 7)->default('#e2e8f0')->after('surface_color');
            $table->string('muted_text_color', 7)->default('#64748b')->after('border_color');

            $table->string('font_size_scale', 20)->default('medium')->after('body_font');
            $table->string('heading_weight', 10)->default('700')->after('font_size_scale');
            $table->string('line_height', 20)->default('normal')->after('heading_weight');
            $table->string('letter_spacing', 20)->default('normal')->after('line_height');

            $table->string('density', 20)->default('comfortable')->after('letter_spacing');
            $table->string('border_radius_preset', 20)->default('rounded')->after('density');
            $table->string('card_style', 20)->default('raised')->after('border_radius_preset');
            $table->string('section_divider', 20)->default('none')->after('card_style');
            $table->string('image_shape', 20)->default('rounded')->after('section_divider');

            $table->boolean('animations_enabled')->default(true)->after('image_shape');
            $table->string('animation_speed', 20)->default('normal')->after('animations_enabled');
        });

        // Update template CHECK constraint to add new templates
        DB::statement("ALTER TABLE website_settings DROP CONSTRAINT IF EXISTS website_settings_template_check");
        DB::statement("ALTER TABLE website_settings ADD CONSTRAINT website_settings_template_check CHECK (template IN ('bold_athletic', 'clean_professional', 'vibrant_playful', 'dark_sport', 'minimal_corporate', 'gradient_modern', 'neon_energy', 'earth_natural'))");

        // Add CHECK constraints for new columns
        DB::statement("ALTER TABLE website_settings ADD CONSTRAINT website_settings_font_size_scale_check CHECK (font_size_scale IN ('small', 'medium', 'large', 'xlarge'))");
        DB::statement("ALTER TABLE website_settings ADD CONSTRAINT website_settings_heading_weight_check CHECK (heading_weight IN ('400', '500', '600', '700', '800', '900'))");
        DB::statement("ALTER TABLE website_settings ADD CONSTRAINT website_settings_line_height_check CHECK (line_height IN ('tight', 'normal', 'relaxed', 'loose'))");
        DB::statement("ALTER TABLE website_settings ADD CONSTRAINT website_settings_letter_spacing_check CHECK (letter_spacing IN ('tight', 'normal', 'wide'))");
        DB::statement("ALTER TABLE website_settings ADD CONSTRAINT website_settings_density_check CHECK (density IN ('compact', 'comfortable', 'spacious'))");
        DB::statement("ALTER TABLE website_settings ADD CONSTRAINT website_settings_border_radius_preset_check CHECK (border_radius_preset IN ('sharp', 'subtle', 'rounded', 'pill'))");
        DB::statement("ALTER TABLE website_settings ADD CONSTRAINT website_settings_card_style_check CHECK (card_style IN ('flat', 'raised', 'bordered', 'glass'))");
        DB::statement("ALTER TABLE website_settings ADD CONSTRAINT website_settings_section_divider_check CHECK (section_divider IN ('none', 'line', 'wave', 'angle', 'curve'))");
        DB::statement("ALTER TABLE website_settings ADD CONSTRAINT website_settings_image_shape_check CHECK (image_shape IN ('square', 'rounded', 'circle', 'blob'))");
        DB::statement("ALTER TABLE website_settings ADD CONSTRAINT website_settings_animation_speed_check CHECK (animation_speed IN ('slow', 'normal', 'fast'))");

        // --- website_sections: per-section background + animation + padding ---
        Schema::table('website_sections', function (Blueprint $table) {
            $table->string('bg_type', 20)->default('inherit')->after('is_enabled');
            $table->string('bg_color', 7)->nullable()->after('bg_type');
            $table->string('bg_gradient_from', 7)->nullable()->after('bg_color');
            $table->string('bg_gradient_to', 7)->nullable()->after('bg_gradient_from');
            $table->smallInteger('bg_gradient_angle')->default(180)->after('bg_gradient_to');
            $table->string('bg_image_path', 500)->nullable()->after('bg_gradient_angle');
            $table->smallInteger('bg_overlay_opacity')->default(60)->after('bg_image_path');
            $table->string('bg_video_url', 500)->nullable()->after('bg_overlay_opacity');
            $table->string('animation', 20)->default('fade-up')->after('bg_video_url');
            $table->string('padding_y', 10)->default('inherit')->after('animation');
            $table->boolean('hidden_on_mobile')->default(false)->after('padding_y');
        });

        DB::statement("ALTER TABLE website_sections ADD CONSTRAINT website_sections_bg_type_check CHECK (bg_type IN ('inherit', 'solid', 'gradient', 'image', 'video'))");
        DB::statement("ALTER TABLE website_sections ADD CONSTRAINT website_sections_animation_check CHECK (animation IN ('none', 'fade-up', 'fade-down', 'fade-left', 'fade-right', 'zoom-in', 'flip', 'bounce'))");
        DB::statement("ALTER TABLE website_sections ADD CONSTRAINT website_sections_padding_y_check CHECK (padding_y IN ('inherit', 'none', 'small', 'medium', 'large', 'xlarge'))");
    }

    public function down(): void
    {
        DB::statement("ALTER TABLE website_settings DROP CONSTRAINT IF EXISTS website_settings_font_size_scale_check");
        DB::statement("ALTER TABLE website_settings DROP CONSTRAINT IF EXISTS website_settings_heading_weight_check");
        DB::statement("ALTER TABLE website_settings DROP CONSTRAINT IF EXISTS website_settings_line_height_check");
        DB::statement("ALTER TABLE website_settings DROP CONSTRAINT IF EXISTS website_settings_letter_spacing_check");
        DB::statement("ALTER TABLE website_settings DROP CONSTRAINT IF EXISTS website_settings_density_check");
        DB::statement("ALTER TABLE website_settings DROP CONSTRAINT IF EXISTS website_settings_border_radius_preset_check");
        DB::statement("ALTER TABLE website_settings DROP CONSTRAINT IF EXISTS website_settings_card_style_check");
        DB::statement("ALTER TABLE website_settings DROP CONSTRAINT IF EXISTS website_settings_section_divider_check");
        DB::statement("ALTER TABLE website_settings DROP CONSTRAINT IF EXISTS website_settings_image_shape_check");
        DB::statement("ALTER TABLE website_settings DROP CONSTRAINT IF EXISTS website_settings_animation_speed_check");

        Schema::table('website_settings', function (Blueprint $table) {
            $table->dropColumn([
                'background_color', 'surface_color', 'border_color', 'muted_text_color',
                'font_size_scale', 'heading_weight', 'line_height', 'letter_spacing',
                'density', 'border_radius_preset', 'card_style', 'section_divider', 'image_shape',
                'animations_enabled', 'animation_speed',
            ]);
        });

        DB::statement("ALTER TABLE website_settings DROP CONSTRAINT IF EXISTS website_settings_template_check");
        DB::statement("ALTER TABLE website_settings ADD CONSTRAINT website_settings_template_check CHECK (template IN ('bold_athletic', 'clean_professional', 'vibrant_playful'))");

        DB::statement("ALTER TABLE website_sections DROP CONSTRAINT IF EXISTS website_sections_bg_type_check");
        DB::statement("ALTER TABLE website_sections DROP CONSTRAINT IF EXISTS website_sections_animation_check");
        DB::statement("ALTER TABLE website_sections DROP CONSTRAINT IF EXISTS website_sections_padding_y_check");

        Schema::table('website_sections', function (Blueprint $table) {
            $table->dropColumn([
                'bg_type', 'bg_color', 'bg_gradient_from', 'bg_gradient_to',
                'bg_gradient_angle', 'bg_image_path', 'bg_overlay_opacity', 'bg_video_url',
                'animation', 'padding_y', 'hidden_on_mobile',
            ]);
        });
    }
};
