<?php
/**
 * Provides the admin header for the plugin.
 *
 * @link       https://mementor.no
 * @since      1.0.0
 *
 * @package    TTSWP
 * @subpackage Mementor_TTS/admin/partials
 */

// Important: This file is meant to be included from WordPress admin pages
// where functions like is_admin(), get_current_screen(), wp_die(), etc.
// are already defined. It should not be accessed directly.

// If this file is called directly, abort.
if (!defined('ABSPATH')) {
    die;
}

// Check if we're in the admin area
if (!is_admin()) {
    return;
}

// Get the current admin screen
$screen = get_current_screen();
if (!$screen) {
    return;
}

// Only show the header on plugin pages - ensure base is not null
if (!isset($screen->base) || strpos($screen->base ?? '', 'mementor-tts') === false) {
    return;
}

// Check if current user can manage options
if (!current_user_can('manage_options')) {
    wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'text-to-speech-tts'));
}

// Define allowed tabs
$allowed_tabs = array('dashboard', 'settings', 'voices', 'player', 'shortcodes', 'statistics', 'advanced');
$nonce_action = 'mementor_tts_nav_tab'; // Define nonce action

// Determine current tab with sanitization and validation, including nonce check
$current_tab = 'dashboard'; // Default tab
if (isset($_GET['tab'])) {
    $tab = sanitize_key($_GET['tab']);
    // Verify nonce if tab parameter is set
    if (isset($_GET['_wpnonce']) && wp_verify_nonce(sanitize_key($_GET['_wpnonce']), $nonce_action)) {
        if (in_array($tab, $allowed_tabs)) {
            $current_tab = $tab;
        }
    } else {
        // Optional: Handle invalid nonce, maybe add an admin notice
    }
}

?>
<div class="wrap mementor-tts-admin">
    <div class="mementor-tts-header">
        <div class="mementor-tts-logo">
            <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=mementor-tts'), $nonce_action)); ?>">
                <img src="<?php echo esc_url(plugin_dir_url(dirname(__FILE__)) . 'images/logo.svg'); ?>" alt="<?php echo esc_attr__('Mementor Text to Speech', 'text-to-speech-tts'); ?>" />
            </a>
        </div>
        <div class="mementor-tts-header-actions">
            <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=mementor-tts-settings'), $nonce_action)); ?>" class="button button-secondary">
                <span class="dashicons dashicons-admin-settings"></span>
                <?php echo esc_html__('Settings', 'text-to-speech-tts'); ?>
            </a>
            <a href="https://mementor.no/support/" class="button button-secondary" target="_blank">
                <span class="dashicons dashicons-editor-help"></span>
                <?php echo esc_html__('Support', 'text-to-speech-tts'); ?>
            </a>
        </div>
    </div>
    
    <div class="mementor-tts-nav-tabs-wrapper">
        <nav class="nav-tab-wrapper mementor-tts-nav-tabs">
            <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=mementor-tts'), $nonce_action)); ?>" class="nav-tab <?php echo esc_attr($current_tab === 'dashboard' ? 'nav-tab-active' : ''); ?>">
                <span class="dashicons dashicons-dashboard"></span>
                <?php echo esc_html__('Dashboard', 'text-to-speech-tts'); ?>
            </a>
            <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=mementor-tts-settings'), $nonce_action)); ?>" class="nav-tab <?php echo esc_attr($current_tab === 'settings' ? 'nav-tab-active' : ''); ?>">
                <span class="dashicons dashicons-admin-settings"></span>
                <?php echo esc_html__('Settings', 'text-to-speech-tts'); ?>
            </a>
            <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=mementor-tts-voices'), $nonce_action)); ?>" class="nav-tab <?php echo esc_attr($current_tab === 'voices' ? 'nav-tab-active' : ''); ?>">
                <span class="dashicons dashicons-microphone"></span>
                <?php echo esc_html__('Voices', 'text-to-speech-tts'); ?>
            </a>
            <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=mementor-tts-shortcodes'), $nonce_action)); ?>" class="nav-tab <?php echo esc_attr($current_tab === 'shortcodes' ? 'nav-tab-active' : ''); ?>">
                <span class="dashicons dashicons-shortcode"></span>
                <?php echo esc_html__('Shortcodes', 'text-to-speech-tts'); ?>
            </a>
        </nav>
    </div>
    
    <div class="mementor-tts-admin-content">
        <!-- Content will be added here by individual page templates -->
    </div>
</div> 