<?php

/**
 * Shois Chat Button — Admin Page
 *
 * Registers the WordPress admin menu and enqueues admin assets.
 *
 * @package ShoisChatButton
 */
// Prevent direct access.
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Register the admin menu page.
 *
 * @return void
 */
function shcb_register_admin_menu() {
    $hook = add_menu_page(
        esc_html__( 'Shois Chat Button', 'shois-chat-button' ),
        esc_html__( 'Shois Chat', 'shois-chat-button' ),
        'manage_options',
        'shois-chat-button',
        'shcb_render_admin_page',
        // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Required for inline SVG admin menu icon as per WordPress add_menu_page() convention.
        'data:image/svg+xml;base64,' . base64_encode( '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>' ),
        58
    );
    add_action( 'load-' . $hook, 'shcb_load_admin_page' );
}

add_action( 'admin_menu', 'shcb_register_admin_menu' );
/**
 * Callback fired when the admin page is loaded.
 * Enqueues scripts and styles only on our page.
 *
 * @return void
 */
function shcb_load_admin_page() {
    add_action( 'admin_enqueue_scripts', 'shcb_enqueue_admin_assets' );
}

/**
 * Enqueue admin scripts and styles.
 *
 * @return void
 */
function shcb_enqueue_admin_assets() {
    $asset_dir = SHCB_PLUGIN_DIR . 'admin/build/';
    $asset_url = SHCB_PLUGIN_URL . 'admin/build/';
    // Load asset manifest for versioning.
    $asset_file = $asset_dir . 'index.asset.php';
    $asset_meta = ( file_exists( $asset_file ) ? require $asset_file : array(
        'dependencies' => array(
            'wp-element',
            'wp-api-fetch',
            'wp-i18n',
            'wp-components'
        ),
        'version'      => SHCB_VERSION,
    ) );
    // Admin JS.
    wp_enqueue_script(
        'shcb-admin-js',
        $asset_url . 'index.js',
        $asset_meta['dependencies'],
        $asset_meta['version'],
        true
    );
    // Localize script data.
    wp_localize_script( 'shcb-admin-js', 'shcbAdmin', array(
        'restUrl'    => esc_url_raw( rest_url( SHCB_REST_NAMESPACE ) ),
        'nonce'      => wp_create_nonce( 'wp_rest' ),
        'pluginUrl'  => esc_url( SHCB_PLUGIN_URL ),
        'version'    => esc_attr( SHCB_VERSION ),
        'siteUrl'    => esc_url( home_url( '/' ) ),
        'adminUrl'   => esc_url( admin_url() ),
        'isPro'      => function_exists( 'scb_fs' ) && scb_fs()->can_use_premium_code__premium_only(),
        'upgradeUrl' => ( function_exists( 'scb_fs' ) ? esc_url( scb_fs()->get_upgrade_url() ) : '' ),
    ) );
    // Set script translations.
    wp_set_script_translations( 'shcb-admin-js', 'shois-chat-button', SHCB_PLUGIN_DIR . 'languages' );
    // Admin CSS.
    $css_file = $asset_dir . 'index.css';
    if ( file_exists( $css_file ) ) {
        wp_enqueue_style(
            'shcb-admin-css',
            $asset_url . 'index.css',
            array('wp-components'),
            $asset_meta['version']
        );
    }
    // Add inline CSS overrides for fullscreen React app experience.
    $admin_override_css = '#wpcontent{padding-left:0!important;}#wpbody-content{padding-bottom:0!important;}#wpfooter{display:none!important;}.update-nag,.notice:not(.shcb-notice){display:none!important;}';
    wp_add_inline_style( 'shcb-admin-css', wp_strip_all_tags( $admin_override_css ) );
    // WordPress components stylesheet.
    wp_enqueue_style( 'wp-components' );
    // Enable Media Library support.
    wp_enqueue_media();
    // Enqueue widget base CSS and all template CSS files for Live Preview accuracy.
    $widget_css_url = SHCB_PLUGIN_URL . 'widget/css/widget-base.css';
    wp_enqueue_style(
        'shcb-widget-base',
        $widget_css_url,
        array(),
        SHCB_VERSION
    );
    $template_dir = SHCB_PLUGIN_DIR . 'widget/templates/';
    $template_url = SHCB_PLUGIN_URL . 'widget/templates/';
    $templates = glob( $template_dir . '*.css' );
    if ( !empty( $templates ) ) {
        $free_templates = array(
            'classic-green',
            'gradient-sunset',
            'minimal-white',
            'ivory-prism',
            'soft'
        );
        foreach ( $templates as $tpl_file ) {
            $tpl_slug = basename( $tpl_file, '.css' );
            if ( in_array( $tpl_slug, $free_templates, true ) ) {
                // Load Free Templates.
                wp_enqueue_style(
                    'shcb-tpl-' . $tpl_slug,
                    $template_url . $tpl_slug . '.css',
                    array('shcb-widget-base'),
                    SHCB_VERSION
                );
            } else {
            }
        }
    }
}

/**
 * Render the admin page container.
 * React mounts here.
 *
 * @return void
 */
function shcb_render_admin_page() {
    echo '<div id="shcb-admin-root" class="shcb-admin-wrapper"></div>';
}

/**
 * Add plugin action links.
 *
 * @param array $links Existing plugin action links.
 * @return array Modified links.
 */
function shcb_plugin_action_links(  $links  ) {
    $settings_link = sprintf( '<a href="%s">%s</a>', esc_url( admin_url( 'admin.php?page=shois-chat-button' ) ), esc_html__( 'Settings', 'shois-chat-button' ) );
    array_unshift( $links, $settings_link );
    return $links;
}

add_filter( 'plugin_action_links_' . plugin_basename( SHCB_PLUGIN_FILE ), 'shcb_plugin_action_links' );