<?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
    {
        // Predefined themes
        Schema::create('agency_themes', function (Blueprint $table) {
            $table->id();
            $table->string('name'); // modern, classic, minimal, luxury
            $table->string('display_name');
            $table->text('description')->nullable();
            $table->string('preview_image')->nullable();
            $table->json('default_settings'); // colors, fonts, layout
            $table->json('available_sections'); // hangi sectionlar kullanılabilir
            $table->boolean('is_active')->default(true);
            $table->integer('sort_order')->default(0);
            $table->timestamps();

            $table->index(['is_active', 'sort_order']);
        });

        // Agency page customization
        Schema::create('agency_pages', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('agency_id')->unique();
            $table->unsignedBigInteger('theme_id');

            // Theme Customization
            $table->json('theme_overrides')->nullable(); // tema üzerinde yapılan değişiklikler

            // SEO
            $table->string('meta_title')->nullable();
            $table->text('meta_description')->nullable();
            $table->string('meta_keywords')->nullable();
            $table->string('og_image')->nullable();

            // Layout Settings
            $table->enum('layout_style', ['boxed', 'full-width'])->default('full-width');

            // Custom CSS/JS
            $table->text('custom_css')->nullable();
            $table->text('custom_js')->nullable();
            $table->text('header_scripts')->nullable();
            $table->text('footer_scripts')->nullable();

            // Status
            $table->boolean('is_published')->default(false);
            $table->timestamp('published_at')->nullable();

            // Audit
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();

            $table->foreign('agency_id')->references('id')->on('agencies')->onDelete('cascade');
            $table->foreign('theme_id')->references('id')->on('agency_themes')->onDelete('restrict');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');

            $table->index(['agency_id', 'is_published']);
        });

        // Page sections (dinamik içerik blokları)
        Schema::create('agency_page_sections', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('agency_page_id');

            // Section Type
            $table->string('section_type'); // hero, about, services, tours, categories, testimonials, blog, contact, custom
            $table->string('section_key')->nullable(); // unique identifier

            // Content
            $table->string('title')->nullable();
            $table->text('subtitle')->nullable();
            $table->text('content')->nullable();
            $table->json('settings')->nullable(); // section-specific settings
            $table->json('data')->nullable(); // section data (images, links, etc)

            // Display Settings
            $table->integer('sort_order')->default(0);
            $table->boolean('is_active')->default(true);
            $table->string('background_color')->nullable();
            $table->string('background_image')->nullable();
            $table->enum('container_width', ['full', 'wide', 'normal', 'narrow'])->default('normal');
            $table->string('padding_top')->nullable(); // sm, md, lg, xl
            $table->string('padding_bottom')->nullable();

            // Conditional Display
            $table->boolean('show_on_mobile')->default(true);
            $table->boolean('show_on_tablet')->default(true);
            $table->boolean('show_on_desktop')->default(true);

            // Audit
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();

            $table->foreign('agency_page_id')->references('id')->on('agency_pages')->onDelete('cascade');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');

            $table->index(['agency_page_id', 'is_active', 'sort_order']);
            $table->index(['section_type', 'is_active']);
        });

        // Page revisions (değişiklik takibi)
        Schema::create('agency_page_revisions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('agency_page_id');
            $table->json('page_data'); // full snapshot
            $table->json('sections_data'); // sections snapshot
            $table->string('change_summary')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->timestamps();

            $table->foreign('agency_page_id')->references('id')->on('agency_pages')->onDelete('cascade');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');

            $table->index(['agency_page_id', 'created_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('agency_page_revisions');
        Schema::dropIfExists('agency_page_sections');
        Schema::dropIfExists('agency_pages');
        Schema::dropIfExists('agency_themes');
    }
};
