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

/**
 * Enqueues admin CSS/JS on VisitorLens pages only.
 */
class VLENS_Admin_Assets {

    public function __construct() {
        add_action( 'admin_enqueue_scripts', [ $this, 'enqueue' ] );
        add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_menu_highlight' ] );
    }

    /**
     * Sidebar menu highlight — loaded on every admin screen (not gated to
     * VisitorLens pages like enqueue() below) since the WP admin menu itself
     * is global; the animation is what lets it be spotted at a glance.
     */
    public function enqueue_menu_highlight() {
        wp_enqueue_style(
            'vlens-menu-highlight',
            VLENS_PLUGIN_URL . 'admin/css/menu-highlight.css',
            [],
            $this->ver( 'admin/css/menu-highlight.css' )
        );
    }

    /**
     * Returns the filemtime of a plugin asset as version string.
     * Falls back to VLENS_VERSION if the file doesn't exist.
     */
    private function ver( $relative_path ) {
        $abs = VLENS_PLUGIN_DIR . $relative_path;
        return file_exists( $abs ) ? (string) filemtime( $abs ) : VLENS_VERSION;
    }

    public function enqueue( $hook ) {
        // Only load on our plugin pages
        if ( strpos( $hook, 'visitorlens' ) === false ) {
            return;
        }

        // Register Chart.js (bundled locally)
        wp_register_script(
            'chartjs',
            VLENS_PLUGIN_URL . 'admin/js/chart.umd.min.js',
            [],
            '4.5.1',
            true
        );

        wp_enqueue_style(
            'vlens-admin',
            VLENS_PLUGIN_URL . 'admin/css/admin.css',
            [],
            $this->ver( 'admin/css/admin.css' )
        );

        wp_enqueue_script(
            'vlens-admin',
            VLENS_PLUGIN_URL . 'admin/js/admin.js',
            [],
            $this->ver( 'admin/js/admin.js' ),
            true
        );

        wp_localize_script( 'vlens-admin', 'vlensAdmin', [
            'restBase'   => rest_url( 'vlens/v1/' ),
            'nonce'      => wp_create_nonce( 'wp_rest' ),
            'adminUrl'   => admin_url(),
            'siteUrl'    => site_url(),
            'userId'     => get_current_user_id(),
            'pluginUrl'  => VLENS_PLUGIN_URL,
            'upgradeUrl' => 'https://myrecorp.com/visitor-lens/',
        ] );

        // Dashboard page (main menu + its submenu alias)
        if ( $hook === 'toplevel_page_visitorlens' ) {
            wp_enqueue_script( 'chartjs' );
            wp_enqueue_script(
                'vlens-dashboard',
                VLENS_PLUGIN_URL . 'admin/js/dashboard.js',
                [ 'vlens-admin', 'chartjs' ],
                $this->ver( 'admin/js/dashboard.js' ),
                true
            );
        }

        // Statistics page
        if ( strpos( $hook, 'visitorlens-stats' ) !== false ) {
            wp_enqueue_style(
                'vlens-stats',
                VLENS_PLUGIN_URL . 'admin/css/stats.css',
                [ 'vlens-admin' ],
                $this->ver( 'admin/css/stats.css' )
            );
            wp_enqueue_script( 'chartjs' );
            wp_enqueue_script(
                'vlens-stats',
                VLENS_PLUGIN_URL . 'admin/js/stats.js',
                [ 'vlens-admin', 'chartjs' ],
                $this->ver( 'admin/js/stats.js' ),
                true
            );
        }

        // Settings page
        if ( strpos( $hook, 'visitorlens-settings' ) !== false ) {
            wp_enqueue_script(
                'vlens-settings',
                VLENS_PLUGIN_URL . 'admin/js/settings.js',
                [ 'vlens-admin' ],
                $this->ver( 'admin/js/settings.js' ),
                true
            );
        }

        // Replay page
        if ( strpos( $hook, 'visitorlens-replay' ) !== false ) {
            wp_enqueue_style(
                'vlens-replay',
                VLENS_PLUGIN_URL . 'admin/css/replay.css',
                [ 'vlens-admin' ],
                $this->ver( 'admin/css/replay.css' )
            );
            wp_enqueue_script(
                'vlens-replay',
                VLENS_PLUGIN_URL . 'admin/js/replay.js',
                [ 'vlens-admin' ],
                $this->ver( 'admin/js/replay.js' ),
                true
            );
        }

        // Premium sidebar — shown on dashboard, stats, replay pages (free users only)
        $is_sidebar_page = ( $hook === 'toplevel_page_visitorlens'
            || strpos( $hook, 'visitorlens-stats' )  !== false
            || strpos( $hook, 'visitorlens-replay' ) !== false )
            && ! apply_filters( 'vlens_is_premium', false );

        if ( $is_sidebar_page ) {
            wp_enqueue_style(
                'vlens-premium-sidebar',
                VLENS_PLUGIN_URL . 'admin/css/vsr-premium-sidebar.css',
                [ 'vlens-admin' ],
                $this->ver( 'admin/css/vsr-premium-sidebar.css' )
            );
            wp_enqueue_script(
                'vlens-premium-sidebar',
                VLENS_PLUGIN_URL . 'admin/js/vsr-premium-sidebar.js',
                [ 'vlens-admin' ],
                $this->ver( 'admin/js/vsr-premium-sidebar.js' ),
                true
            );
        }

        // Premium can enqueue additional admin assets
        do_action( 'vlens_enqueue_admin_assets', $hook );
    }
}
