<?php
/**
 * Navigation component for admin pages
 *
 * @link       https://mementor.no
 * @since      1.0.0
 *
 * @package    TTSWP
 * @subpackage Mementor_TTS/admin/partials
 */

// Prevent direct access to this file
if (!defined('ABSPATH')) {
    exit;
}

// Define allowed pages for validation
$allowed_pages = array(
    'text-to-speech-tts-settings',
    'text-to-speech-tts-content',
    'text-to-speech-tts-voices',
    'text-to-speech-tts-player',
    'text-to-speech-tts-shortcodes',
    'text-to-speech-tts-statistics',
    'text-to-speech-tts-my-stats-voices',
    'text-to-speech-tts-generator',
    'text-to-speech-tts-advanced',
    'text-to-speech-tts-affiliate',
    'text-to-speech-tts-upgrade',
    'text-to-speech-tts-pro-help',
    'text-to-speech-tts-external-apis',
    'text-to-speech-tts-account'
);

// Define the nonce action
$nonce_action = 'mementor_tts_nav_page';

// Get current page
$current_page = '';
if (isset($_GET['page'])) {
    $page = sanitize_text_field(wp_unslash($_GET['page']));
    
    // Ensure page is not null before proceeding
    if ($page !== null && $page !== '') {
        // Validate against allowed pages
        if (in_array($page, $allowed_pages)) {
            $current_page = str_replace('text-to-speech-tts-', '', $page);
            
            // Special handling for my-stats-voices page
            if ($page === 'text-to-speech-tts-my-stats-voices') {
                $current_page = 'my-stats-voices';
            }
        }
    }
}

// Check if API key is set
$has_api_key = !empty(get_option('mementor_tts_api_key'));

// Make sure plugin functions are available
if (!function_exists('is_plugin_active')) {
    include_once(ABSPATH . 'wp-admin/includes/plugin.php');
}

// Check if user has a paid plan via SaaS
$is_pro_active = ttswp_has_paid_plan();

// Define which tabs should be locked when no API key is present
$locked_tabs = array(); // Always empty to prevent locking

// Menu pages that should show the navigation
$menu_pages = array(
    'text-to-speech-tts-settings',
    'text-to-speech-tts-content',
    'text-to-speech-tts-voices',
    'text-to-speech-tts-player',
    'text-to-speech-tts-shortcodes',
    'text-to-speech-tts-statistics',
    'text-to-speech-tts-my-stats-voices',
    'text-to-speech-tts-generator',
    'text-to-speech-tts-advanced',
    'text-to-speech-tts-affiliate',
    'text-to-speech-tts-upgrade',
    'text-to-speech-tts-pro-help',
    'text-to-speech-tts-account'
);
?>

<!-- Navigation Menu -->
<div class="mementor-tts-admin-nav">
    <div class="mementor-tts-nav-left">
    <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=text-to-speech-tts-settings'), $nonce_action)); ?>" class="mementor-tts-nav-item <?php echo $current_page === 'settings' ? 'active' : ''; ?>">
        <span class="dashicons dashicons-admin-settings"></span>
        <?php esc_html_e('Settings', 'text-to-speech-tts'); ?>
    </a>
    
    <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=text-to-speech-tts-content'), $nonce_action)); ?>" class="mementor-tts-nav-item <?php echo $current_page === 'content' ? 'active' : ''; ?>">
        <span class="dashicons dashicons-media-text"></span>
        <?php esc_html_e('Content', 'text-to-speech-tts'); ?>
    </a>
    
    <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=text-to-speech-tts-voices'), $nonce_action)); ?>" class="mementor-tts-nav-item <?php echo $current_page === 'voices' ? 'active' : ''; ?>">
        <span class="dashicons dashicons-microphone"></span>
        <?php esc_html_e('Voices', 'text-to-speech-tts'); ?>
    </a>
    
    <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=text-to-speech-tts-player'), $nonce_action)); ?>" class="mementor-tts-nav-item <?php echo $current_page === 'player' ? 'active' : ''; ?>">
        <span class="dashicons dashicons-controls-play"></span>
        <?php esc_html_e('Player', 'text-to-speech-tts'); ?>
    </a>
    
    <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=text-to-speech-tts-generator'), $nonce_action)); ?>" class="mementor-tts-nav-item <?php echo $current_page === 'generator' ? 'active' : ''; ?>">
        <span class="dashicons dashicons-shortcode"></span>
        <?php esc_html_e('Generator', 'text-to-speech-tts'); ?>
    </a>

    <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=text-to-speech-tts-my-stats-voices'), $nonce_action)); ?>" class="mementor-tts-nav-item <?php echo $current_page === 'my-stats-voices' ? 'active' : ''; ?>">
        <span class="dashicons dashicons-chart-line"></span>
        <?php esc_html_e('My Stats', 'text-to-speech-tts'); ?>
    </a>
    
    <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=text-to-speech-tts-statistics'), $nonce_action)); ?>" class="mementor-tts-nav-item <?php echo $current_page === 'statistics' ? 'active' : ''; ?>">
        <span class="dashicons dashicons-chart-bar"></span>
        <?php esc_html_e('Audio Statistics', 'text-to-speech-tts'); ?>
    </a>

    <a href="<?php echo esc_url(admin_url('admin.php?page=text-to-speech-tts-account')); ?>" class="mementor-tts-nav-item <?php echo ('account' === $current_page) ? 'active' : ''; ?>">
        <span class="dashicons dashicons-admin-users"></span>
        <?php esc_html_e('My Plan & Credits', 'text-to-speech-tts'); ?>
    </a>
    </div>
    
    <div class="mementor-tts-nav-right">
        <?php if ($is_pro_active): // Show PRO Support for any PRO user ?>
            <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=text-to-speech-tts-pro-help'), $nonce_action)); ?>" class="mementor-tts-nav-item mementor-tts-help-nav-button <?php echo $current_page === 'pro-help' ? 'active' : ''; ?>">
                <span class="dashicons dashicons-sos"></span>
                <?php esc_html_e('PRO Support', 'text-to-speech-tts'); ?>
            </a>
        <?php endif; ?>
        <?php if (!$is_pro_active): // Show upgrade button for non-PRO users ?>
            <a href="<?php echo esc_url(admin_url('admin.php?page=text-to-speech-tts-account#upgrade')); ?>" class="mementor-tts-nav-item mementor-tts-upgrade-nav-button">
                <span class="dashicons dashicons-star-filled"></span>
                <?php esc_html_e('Upgrade to Pro', 'text-to-speech-tts'); ?>
            </a>
        <?php endif; ?>
    </div>
</div> 