<?php
/**
 * Copyright (C) 2026 Plugixa (https://plugixa.com)
 * License: GNU General Public License v2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 */

namespace PlugixaRecipe\Admin;

defined( 'ABSPATH' ) || exit;

/**
 * Admin Class - Handles admin menu and asset loading
 */
class Admin {

    public function add_admin_menu(): void {
        $icon = 'dashicons-food';

        add_menu_page(
            __('Plugixa Recipe', 'plugixarecipe'),
            __('Recipes', 'plugixarecipe'),
            'edit_posts',
            'plugixa-recipe',
            [$this, 'render_app'],
            $icon,
            26
        );

        $submenus = [
            'plugixa-recipe'              => __('Dashboard', 'plugixarecipe'),
            'plugixa-recipe-recipes'      => __('Recipes', 'plugixarecipe'),
            'plugixa-recipe-categories'   => __('Categories', 'plugixarecipe'),
            'plugixa-recipe-cuisines'     => __('Cuisines', 'plugixarecipe'),
            'plugixa-recipe-reviews'      => __('Reviews', 'plugixarecipe'),
            'plugixa-recipe-collections'  => __('Collections', 'plugixarecipe'),
            'plugixa-recipe-views'        => __('Views', 'plugixarecipe'),
            'plugixa-recipe-shortcodes'   => __('Shortcodes', 'plugixarecipe'),
            'plugixa-recipe-settings'     => __('Settings', 'plugixarecipe'),
        ];

        foreach ($submenus as $slug => $title) {
            add_submenu_page(
                'plugixa-recipe',
                $title,
                $title,
                'edit_posts',
                $slug,
                [$this, 'render_app']
            );
        }
    }

    public function render_app(): void {
        echo '<div id="plugixarecipe-root"></div>';
    }

    public function enqueue_assets(string $hook): void {
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only page routing, no state change.
        $page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';
        if (strpos($page, 'plugixa-recipe') !== 0) {
            return;
        }

        $assets = new Assets(
            PLUGIXARECIPE_PATH,
            PLUGIXARECIPE_URL,
            PLUGIXARECIPE_VERSION
        );
        $assets->enqueue_app_assets();
    }
}
