<?php

/**
 * Migration 053: Add theme section support
 *
 * Extends iw_section_types to support theme-bound sections:
 * - Adds 'theme' to the source ENUM
 * - Adds theme_slug column for binding sections to specific themes
 */

use App\Database\Migration;

class AddThemeSectionSupport extends Migration
{
    public function up(): void
    {
        $exists = $this->db->fetch(
            "SELECT COLUMN_NAME FROM information_schema.COLUMNS
             WHERE TABLE_SCHEMA = DATABASE()
               AND TABLE_NAME = 'iw_section_types'
               AND COLUMN_NAME = 'theme_slug'
             LIMIT 1"
        );

        if (!$exists) {
            $this->db->query("ALTER TABLE iw_section_types
                MODIFY COLUMN source ENUM('core','plugin','theme') DEFAULT 'core'");

            $this->db->query("ALTER TABLE iw_section_types
                ADD COLUMN theme_slug VARCHAR(100) DEFAULT NULL AFTER plugin_slug");
        }
    }

    public function down(): void
    {
        $this->db->query("ALTER TABLE iw_section_types
            MODIFY COLUMN source ENUM('core','plugin') DEFAULT 'core'");

        $this->db->query("ALTER TABLE iw_section_types
            DROP COLUMN theme_slug");
    }
}
