<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * Creates the publishing_pages table for dynamic CMS functionality.
     * Stores JSON-based content blocks for flexible page building.
     *
     * Requirements: 4.1, 4.2, 4.5, 10.1, 10.2
     */
    public function up(): void
    {
        Schema::create('publishing_pages', function (Blueprint $table) {
            // Primary key
            $table->id();

            // Page Identity
            $table->string('title', 191)->comment('Page title');
            $table->string('slug', 191)->unique()->comment('URL-friendly identifier');

            // Content Structure (JSON-based block system)
            $table->json('content_blocks')->nullable()->comment('Array of content block configurations');
            // Block structure example:
            // [
            //   { "type": "hero", "title": "...", "subtitle": "...", "background": "..." },
            //   { "type": "text", "content": "..." },
            //   { "type": "image_grid", "images": [...], "columns": 3 },
            //   { "type": "carousel", "slides": [...] },
            //   { "type": "cta", "text": "...", "url": "...", "style": "primary" },
            //   { "type": "split_screen", "layout": "left", "image": "...", "content": "..." },
            //   { "type": "card_section", "cards": [...] },
            //   { "type": "quote", "text": "...", "author": "..." }
            // ]

            // Theme Configuration
            $table->json('theme_colors')->nullable()->comment('Custom theme colors');
            // Example: { "primary": "#10b981", "secondary": "#3b82f6", "background": "#ffffff" }

            // SEO Configuration
            $table->json('seo_meta')->nullable()->comment('SEO metadata');
            // Example: { "meta_title": "...", "meta_description": "...", "keywords": [...], "og_image": "..." }

            // Layout Settings
            $table->string('layout', 50)->default('default')->comment('Page layout template');
            $table->boolean('show_breadcrumbs')->default(true)->comment('Display breadcrumb navigation');
            $table->boolean('show_sidebar')->default(false)->comment('Display sidebar');

            // Publication Status
            $table->boolean('is_published')->default(false)->comment('Public visibility');
            $table->timestamp('published_at')->nullable()->comment('When page was published');

            // Authorship
            $table->foreignId('created_by')
                ->nullable()
                ->constrained('users')
                ->onDelete('set null')
                ->comment('User who created the page');

            $table->foreignId('updated_by')
                ->nullable()
                ->constrained('users')
                ->onDelete('set null')
                ->comment('User who last updated the page');

            // Statistics
            $table->integer('view_count')->default(0)->comment('Number of views');

            // Timestamps and Soft Deletes
            $table->timestamps();
            $table->softDeletes();

            // Indexes
            $table->index('slug');
            $table->index('is_published');
            $table->index('created_by');
            $table->index('published_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('publishing_pages');
    }
};
