<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Registers the WP admin menu pages.
 */
class VLENS_Admin_Menu {

    public function __construct() {
        add_action( 'admin_menu', [ $this, 'register_menus' ] );
        add_filter( 'admin_body_class', [ $this, 'add_body_classes' ] );
        add_filter( 'user_has_cap', [ $this, 'map_admin_capability' ], 10, 1 );
    }

    /**
     * Anyone who can manage_options implicitly holds VLENS_CAP, so the menu is
     * gated on a plugin-owned capability instead of manage_options. Premium's
     * team module grants the same capability to team members — that way a
     * non-admin team member can open the pages without being handed real
     * site-admin privileges.
     */
    public function map_admin_capability( $allcaps ) {
        if ( ! empty( $allcaps['manage_options'] ) ) {
            $allcaps[ VLENS_CAP ] = true;
        }
        return $allcaps;
    }

    /**
     * Adds 'auto-fold' on the Session Replay page (and any of its variants)
     * so the admin menu starts folded; JS un-folds it on #adminmenuwrap hover.
     * Also adds WP core's own 'folded' class so the native icon-only menu
     * CSS/flyout-submenu behavior applies — 'auto-fold' has no styling of
     * its own, it just marks the page as opted into the auto-fold behavior.
     */
    public function add_body_classes( $classes ) {
        $screen = get_current_screen();
        if ( $screen && strpos( $screen->id, 'visitorlens-replay' ) !== false ) {
            $classes .= ' auto-fold folded ';
        }
        return $classes;
    }

    public function register_menus() {
        // Main menu
        add_menu_page(
            __( 'VisitorLens', 'visitor-lens' ),
            __( 'VisitorLens', 'visitor-lens' ),
            VLENS_CAP,
            'visitorlens',
            [ $this, 'render_dashboard' ],
            'dashicons-visibility',
            30
        );

        // Submenus
        add_submenu_page(
            'visitorlens',
            __( 'Dashboard', 'visitor-lens' ),
            __( 'Dashboard', 'visitor-lens' ),
            VLENS_CAP,
            'visitorlens',
            [ $this, 'render_dashboard' ]
        );

        add_submenu_page(
            'visitorlens',
            __( 'Session Replay', 'visitor-lens' ),
            __( 'Session Replay', 'visitor-lens' ),
            VLENS_CAP,
            'visitorlens-replay',
            [ $this, 'render_replay' ]
        );

        add_submenu_page(
            'visitorlens',
            __( 'Statistics', 'visitor-lens' ),
            __( 'Statistics', 'visitor-lens' ),
            VLENS_CAP,
            'visitorlens-stats',
            [ $this, 'render_stats' ]
        );

        add_submenu_page(
            'visitorlens',
            __( 'Settings', 'visitor-lens' ),
            __( 'Settings', 'visitor-lens' ),
            VLENS_CAP,
            'visitorlens-settings',
            [ $this, 'render_settings' ]
        );

        // Premium can add additional submenus
        do_action( 'vlens_register_admin_menus' );
    }

    public function render_dashboard() {
        include VLENS_PLUGIN_DIR . 'admin/views/dashboard.php';
    }

    public function render_replay() {
        include VLENS_PLUGIN_DIR . 'admin/views/replay.php';
    }

    public function render_stats() {
        include VLENS_PLUGIN_DIR . 'admin/views/stats.php';
    }

    public function render_settings() {
        include VLENS_PLUGIN_DIR . 'admin/views/settings.php';
    }

}
