<?php

/**
 * LolaCore - Menu
 *
 * @package LolaCore
 * @license GPL-2.0-or-later
 */

declare(strict_types=1);

namespace LolaCore\Admin;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Menu
 *
 * Registers the LolaCore admin menu and renders the main page.
 */
class Menu
{
    /**
     * Register the admin menu.
     */
    public function register(): void
    {
        \add_action('admin_menu', [$this, 'addMenuPage']);
        \add_action('admin_enqueue_scripts', [$this, 'enqueueFonts']);
    }

    /**
     * Enqueue Google Fonts for the LolaCore admin interface.
     */
    public function enqueueFonts(string $hookSuffix): void
    {
        // Only load fonts and styles on our settings pages (main or addon submenus)
        if ($hookSuffix === 'toplevel_page_lolacore' || strpos($hookSuffix, 'lolacore_page_') === 0) {
            \wp_enqueue_style(
                'lolacore-fonts',
                'https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@500;600;700&family=Plus+Jakarta+Sans:wght@400;500;700;800&display=swap',
                [],
                LOLACORE_VERSION
            );
            
            // Addon pages also need the core admin CSS for the design system classes
            \wp_enqueue_style(
                'lolacore-admin',
                \plugin_dir_url(dirname(__DIR__)) . 'assets/css/lolacore-admin.css',
                ['lolacore-fonts'],
                LOLACORE_VERSION
            );
        }
    }

    /**
     * Add the top-level menu page.
     */
    public function addMenuPage(): void
    {
        // Add top-level menu "Lola"
        \add_menu_page(
            \__('Lola', 'lolacore'),    // Page title
            \__('Lola', 'lolacore'),    // Menu title
            'manage_options',           // Capability required
            'lolacore',                 // Menu slug
            [$this, 'renderPage'],      // Callback function
            'dashicons-format-chat',    // Icon (dashicons or base64 SVG)
            80                          // Position (after Settings)
        );

        // Rename the first sub-menu item from "Lola" to "Settings"
        \add_submenu_page(
            'lolacore',
            \__('Settings', 'lolacore'),
            \__('Settings', 'lolacore'),
            'manage_options',
            'lolacore',
            [$this, 'renderPage']
        );

        \add_submenu_page(
            'lolacore',
            \__('Addons', 'lolacore'),
            \__('Addons', 'lolacore'),
            'manage_options',
            'lolacore-addons',
            [$this, 'renderAddonsPage']
        );

        // Add MCP Settings sub-menu
        \add_submenu_page(
            'lolacore',
            \__('MCP Settings — Lola', 'lolacore'),
            \__('MCP Settings', 'lolacore'),
            'manage_options',
            'lolacore-mcp',
            [$this, 'renderMcpPage']
        );

        \add_submenu_page(
            'lolacore',
            \__('Skills — Lola', 'lolacore'),
            \__('Skills', 'lolacore'),
            'manage_options',
            'lolacore-skills',
            [$this, 'renderSkillsPage']
        );

        \add_submenu_page(
            'lolacore',
            \__('Playbooks — Lola', 'lolacore'),
            \__('Playbooks', 'lolacore'),
            'manage_options',
            'lolacore-playbooks',
            [$this, 'renderPlaybooksPage']
        );
    }

    /**
     * Render the main settings page (no tabs, settings is the only view).
     */
    public function renderPage(): void
    {
        if (!\current_user_can('manage_options')) {
            \wp_die(\esc_html__('You do not have sufficient permissions to access this page.', 'lolacore'));
        }

        ?>
        <div class="wrap lolacore-admin-wrap">
            <h1><?php echo \esc_html__('Lola', 'lolacore'); ?></h1>

            <div class="lolacore-tab-content">
                <?php
                $templatePath = \plugin_dir_path(dirname(__DIR__)) . 'templates/settings-page.php';
                if (\file_exists($templatePath)) {
                    require_once $templatePath;
                } else {
                    echo '<p>' . \esc_html__('Settings template not found.', 'lolacore') . '</p>';
                }
                ?>
            </div>
        </div>
        <?php
    }

    /**
     * Render the Addons catalog page.
     */
    public function renderAddonsPage(): void
    {
        if (!\current_user_can('manage_options')) {
            \wp_die(\esc_html__('You do not have sufficient permissions to access this page.', 'lolacore'));
        }

        ?>
        <div class="wrap lolacore-admin-wrap">
            <div class="lolacore-tab-content">
                <?php
                $templatePath = \plugin_dir_path(dirname(__DIR__)) . 'templates/addons-page.php';
                if (\file_exists($templatePath)) {
                    require_once $templatePath;
                } else {
                    echo '<p>' . \esc_html__('Addons template not found.', 'lolacore') . '</p>';
                }
                ?>
            </div>
        </div>
        <?php
    }

    public function renderSkillsPage(): void
    {
        if (!\current_user_can('manage_options')) {
            \wp_die(\esc_html__('You do not have sufficient permissions to access this page.', 'lolacore'));
        }

        $templatePath = __DIR__ . '/views/skills-page.php';
        if (\file_exists($templatePath)) {
            require $templatePath;
            return;
        }

        echo '<p>' . \esc_html__('Skills template not found.', 'lolacore') . '</p>';
    }

    /**
     * Render the Playbooks management page.
     */
    public function renderPlaybooksPage(): void
    {
        if (!\current_user_can('manage_options')) {
            \wp_die(\esc_html__('You do not have sufficient permissions to access this page.', 'lolacore'));
        }

        $templatePath = __DIR__ . '/views/playbooks-page.php';
        if (\file_exists($templatePath)) {
            require $templatePath;
            return;
        }

        echo '<p>' . \esc_html__('Playbooks template not found.', 'lolacore') . '</p>';
    }

    /**
     * Render the MCP settings page.
     */
    public function renderMcpPage(): void
    {
        if (!\current_user_can('manage_options')) {
            \wp_die(\esc_html__('You do not have sufficient permissions to access this page.', 'lolacore'));
        }

        ?>
        <div class="wrap lolacore-admin-wrap">
            <div class="lolacore-tab-content">
                <?php
                $templatePath = __DIR__ . '/views/mcp/page.php';
                if (\file_exists($templatePath)) {
                    require_once $templatePath;
                } else {
                    echo '<p>' . \esc_html__('MCP Settings template not found.', 'lolacore') . '</p>';
                }
                ?>
            </div>
        </div>
        <?php
    }
}
