<?php
/**
 * TitanCart Admin Controller.
 *
 * Registers the WP admin menu, sub-pages, and the single-page
 * application shell that mounts the React admin UI.
 *
 * @package TitanCart\Admin
 */

namespace TitanCart\Admin;

defined( 'ABSPATH' ) || exit;

class AdminController {

    /** @var string Top-level menu slug */
    const MENU_SLUG = 'titancart';

    public function __construct() {
        add_action( 'admin_menu', [ $this, 'register_menu' ] );
        add_action( 'admin_notices', [ $this, 'setup_notice' ] );
    }

    /**
     * Register top-level and sub-menu pages.
     */
    public function register_menu(): void {
        add_menu_page(
            __( 'TitanCart', 'titancart-lite' ),
            __( 'TitanCart', 'titancart-lite' ),
            'manage_options',
            self::MENU_SLUG,
            [ $this, 'render_spa' ],
            'dashicons-cart',
            56 // WP.org R1 🅲: was 25 (between core Comments/Appearance — risks colliding with core positions); 56 sits just below Plugins per the reviewer guideline.
        );

        $pages = [
            'orders'        => __( 'Sales',              'titancart-lite' ),
            'products'      => __( 'Products',           'titancart-lite' ),
            'categories'    => __( 'Categories',         'titancart-lite' ),
            'attributes'    => __( 'Attributes',         'titancart-lite' ),
            'customers'     => __( 'Customers',          'titancart-lite' ),
            'shipping'      => __( 'Shipping',           'titancart-lite' ),
            'payments'      => __( 'Payment Gateways',   'titancart-lite' ),
            'tax'           => __( 'Tax Rules',          'titancart-lite' ),
            'email'         => __( 'Email Templates',    'titancart-lite' ),
            'import-export' => __( 'Import / Export',    'titancart-lite' ),
            'settings'      => __( 'Settings',           'titancart-lite' ),
        ];

        foreach ( $pages as $slug => $label ) {
            add_submenu_page(
                self::MENU_SLUG,
                $label,
                $label,
                'manage_options',
                self::MENU_SLUG . '#/' . $slug,
                [ $this, 'render_spa' ]   // Must output the shell so WP has a valid page.
            );
        }

        // WordPress auto-creates a first submenu that mirrors the top-level page.
        // Rename its label to "Dashboard" so it reads correctly in the menu.
        global $submenu;
        if ( isset( $submenu[ self::MENU_SLUG ][0] ) ) {
            $submenu[ self::MENU_SLUG ][0][0] = __( 'Dashboard', 'titancart-lite' );
        }
    }

    /**
     * Render the SPA shell.
     *
     * The React app mounts into #titancart-app.
     */
    public function render_spa(): void {
        // The SPA-shell chrome CSS (padding suppression) is attached in
        // Assets::enqueue() via wp_add_inline_style (WP.org R1 🅱1) — this
        // method only outputs the mount node.
        echo '<div id="titancart-app"></div>';
    }

    /**
     * Show a notice if the plugin was just activated with no prior data.
     */
    public function setup_notice(): void {
        $screen = get_current_screen();
        if ( ! $screen || $screen->id !== 'toplevel_page_titancart' ) {
            return;
        }
        // Future: onboarding wizard hook.
    }
}
