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

class EMPR_Public {

    public static function init() {
        // Canonical empr_* shortcodes for the five SPA views.
        add_shortcode( 'empr_dashboard',          array( __CLASS__, 'sc_dashboard' ) );
        add_shortcode( 'empr_shifts',             array( __CLASS__, 'sc_shifts' ) );
        add_shortcode( 'empr_availability',       array( __CLASS__, 'sc_availability' ) );

        add_action( 'wp_enqueue_scripts', array( __CLASS__, 'maybe_assets' ) );
    }

    private static function is_roster_page() {
        $ids = array_filter( array(
            get_option('empr_page_dashboard'),
            get_option('empr_page_roster'),
            get_option('empr_page_leave'),
            get_option('empr_page_attendance'),
            get_option('empr_page_availability'),
        ) );
        return ! empty( $ids ) && is_page( $ids );
    }

    public static function maybe_assets() {
        if ( ! self::is_roster_page() ) return;
        wp_enqueue_style(  'empr-fonts',  EMPR_PLUGIN_URL . 'public/fonts/fonts.css', array(), EMPR_VERSION );
        wp_enqueue_style(  'empr-public', EMPR_PLUGIN_URL . 'public/css/app.css',  array(), EMPR_VERSION );
        // Shared with the admin SPA (see EMPR_Admin::assets). Depends on emr-public so it always
        // loads last and can correct that file's own breakpoints without !important.
        wp_enqueue_style(  'empr-responsive', EMPR_PLUGIN_URL . 'shared/css/responsive.css', array( 'empr-public' ), EMPR_VERSION );
        wp_enqueue_script( 'empr-app',    EMPR_PLUGIN_URL . 'public/js/app.js',    array(), EMPR_VERSION, true );
        // Build JS data directly here — do NOT call EMPR_Admin to avoid dependency issues
        wp_localize_script( 'empr-app', 'EMPR', self::js_data() );
    }

    /**
     * Build the JS data payload independently — no dependency on EMPR_Admin.
     */
    public static function js_data() {
        return array(
            'ajaxUrl'  => esc_url_raw( admin_url( 'admin-ajax.php' ) ),
            'nonce'    => wp_create_nonce( 'empr_nonce' ),
            'user'     => array(
                'id'        => get_current_user_id(),
                'name'      => wp_get_current_user()->display_name,
                'isManager' => EMPR_Roles::is_manager(),
                'isAdmin'   => EMPR_Roles::is_admin(),
                'pay_rate'  => (float) get_user_meta( get_current_user_id(), 'empr_pay_rate', true ),
                'jobType'   => EMPR_Roles::job_type(),
            ),
            'settings' => EMPR_Settings::for_js(),
        );
    }

    private static function gate( $view ) {
        if ( is_user_logged_in() ) {
            $uid  = get_current_user_id();
            $user = wp_get_current_user();

            if ( get_user_meta( $uid, 'empr_deactivated', true ) === '1' ) {
                return '<div class="emr-login-wall">' .
                    esc_html__( 'Your account access has been revoked. Please contact your manager.', 'employee-roster' ) .
                    '</div>';
            }

            return '<div id="emr-app" class="emr-app" data-page="' . esc_attr( $view ) . '"></div>';
        }
        return '<div class="emr-login-wall">' .
            wp_kses(
                sprintf(
                    /* translators: %s = login URL */
                    __( 'Please <a href="%s">log in</a> to access the roster.', 'employee-roster' ),
                    esc_url( wp_login_url( get_permalink() ) )
                ),
                array( 'a' => array( 'href' => array() ) )
            ) . '</div>';
    }

    public static function sc_dashboard()    { return self::gate( 'dashboard' ); }
    public static function sc_shifts()       { return self::gate( 'shifts' ); }
    public static function sc_availability() { return self::gate( 'availability' ); }
}
