<?php

use App\Database\Migration;

class CreateMenuTables extends Migration
{
    public function up(): void
    {
        $this->db->query("
            CREATE TABLE IF NOT EXISTS iw_menus (
                id INT AUTO_INCREMENT PRIMARY KEY,
                location VARCHAR(50) NOT NULL,
                name VARCHAR(100) NOT NULL,
                UNIQUE KEY idx_location (location)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
        ");

        $this->db->query("
            CREATE TABLE IF NOT EXISTS iw_menu_items (
                id INT AUTO_INCREMENT PRIMARY KEY,
                menu_id INT NOT NULL,
                parent_id INT DEFAULT NULL,
                label VARCHAR(100) NOT NULL,
                url VARCHAR(500) DEFAULT NULL,
                page_id INT DEFAULT NULL,
                target VARCHAR(20) NOT NULL DEFAULT '_self',
                sort_order INT NOT NULL DEFAULT 0,
                is_visible TINYINT(1) NOT NULL DEFAULT 1,
                INDEX idx_menu (menu_id),
                FOREIGN KEY (menu_id) REFERENCES iw_menus(id) ON DELETE CASCADE,
                FOREIGN KEY (page_id) REFERENCES iw_pages(id) ON DELETE SET NULL
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
        ");
    }

    public function down(): void
    {
        $this->db->query("DROP TABLE IF EXISTS iw_menu_items");
        $this->db->query("DROP TABLE IF EXISTS iw_menus");
    }
}
