<?php
/**
 * Dashboard page - Main overview
 *
 * @link       https://mementor.no
 * @since      1.0.0
 *
 * @package    TTSWP
 * @subpackage Mementor_TTS/admin/partials/pages
 */

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

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

// Get API status
$api_key = get_option('mementor_tts_api_key', '');
$is_api_connected = !empty($api_key);

// Get usage statistics
$total_characters = get_option('mementor_tts_total_characters', 125000);
$monthly_limit = 250000;
$usage_percentage = min(100, round(($total_characters / $monthly_limit) * 100));

// Get white label settings
require_once MEMENTOR_TTS_PLUGIN_DIR . 'admin/class-mementor-tts-admin.php';
$admin = new Mementor_TTS_Admin('mementor-tts', MEMENTOR_TTS_VERSION);
$white_label_logo = $admin->get_white_label_logo();
$white_label_name = $admin->get_white_label_name();

?>

<div class="wrap">
    <?php
    // Include header branding
    require_once MEMENTOR_TTS_PLUGIN_DIR . 'admin/partials/header-branding.php';
    ?>

    <?php include(MEMENTOR_TTS_PLUGIN_DIR . 'admin/partials/navigation.php'); ?>

    <div class="tts-dashboard">
        <!-- Header Section -->
        <div class="tts-header-section">
            <div class="tts-header-content">
                <div class="tts-logo-section">
                    <div class="tts-logo">TTS</div>
                    <div>
                        <h2 class="tts-title">Text to Speech Dashboard</h2>
                        <p class="tts-subtitle">Manage your audio content and settings</p>
                    </div>
                </div>
                <a href="<?php echo esc_url(admin_url('admin.php?page=text-to-speech-tts-account#upgrade')); ?>" class="tts-upgrade-button">
                    <span class="dashicons dashicons-star-filled"></span>
                    Upgrade to Pro
                </a>
            </div>
        </div>

        <!-- Navigation Tabs -->
        <div class="tts-nav-tabs">
            <button class="tts-nav-tab active" data-tab="overview">
                <span class="dashicons dashicons-dashboard"></span>
                Overview
            </button>
            <button class="tts-nav-tab" data-tab="analytics">
                <span class="dashicons dashicons-chart-area"></span>
                Analytics
            </button>
            <button class="tts-nav-tab" data-tab="settings">
                <span class="dashicons dashicons-admin-generic"></span>
                Quick Settings
            </button>
            <button class="tts-nav-tab" data-tab="activity">
                <span class="dashicons dashicons-clock"></span>
                Recent Activity
            </button>
        </div>

        <!-- Tab Content -->
        <div class="tts-tab-content">
            <!-- Overview Tab -->
            <div class="tts-tab-panel active" id="overview-panel">
                <div class="tts-main-grid">
                    <!-- Main Content -->
                    <div class="tts-main-content">
                        <!-- Welcome Card -->
                        <div class="tts-card tts-welcome-card">
                            <div class="tts-welcome-content">
                                <div>
                                    <h3>Welcome to Text to Speech</h3>
                                    <p>Transform your written content into natural-sounding audio with AI-powered voices.</p>
                                    <button class="tts-button tts-button-white">
                                        <span class="dashicons dashicons-microphone"></span>
                                        Generate Your First Audio
                                    </button>
                                </div>
                                <div class="tts-audio-preview">
                                    <span class="dashicons dashicons-format-audio"></span>
                                    <p>Listen to sample</p>
                                </div>
                            </div>
                        </div>

                        <!-- API Settings Card -->
                        <div class="tts-card">
                            <h3 class="tts-card-title">
                                <span style="margin-right: 8px;">🔑</span>
                                API Settings
                            </h3>
                            <div class="tts-form-group">
                                <label class="tts-label">API Service</label>
                                <div class="tts-api-status">
                                    <strong>ElevenLabs</strong>
                                    <span class="tts-status-badge <?php echo $is_api_connected ? 'connected' : 'disconnected'; ?>">
                                        <?php echo $is_api_connected ? 'Connected' : 'Not Connected'; ?>
                                    </span>
                                </div>
                            </div>
                            <div class="tts-form-group">
                                <label class="tts-label">API Key</label>
                                <div class="tts-input-group">
                                    <input type="password" class="tts-input" value="<?php echo $is_api_connected ? '••••••••••••••••••••••••' : ''; ?>" placeholder="Enter your API key">
                                    <button class="tts-input-button">
                                        <span class="dashicons dashicons-visibility"></span>
                                    </button>
                                </div>
                            </div>
                            <div class="tts-form-actions">
                                <button class="tts-button tts-button-primary">Save Changes</button>
                                <a href="#" class="tts-link">Advanced Settings →</a>
                            </div>
                        </div>

                        <!-- Storage Settings Card -->
                        <div class="tts-card">
                            <h3 class="tts-card-title">
                                <span style="margin-right: 8px;">💾</span>
                                Storage Settings
                            </h3>
                            <div class="tts-form-group">
                                <label class="tts-label">Storage Location</label>
                                <select class="tts-select">
                                    <option>Local WordPress Storage</option>
                                    <option disabled>Amazon S3 (Pro)</option>
                                    <option disabled>Dropbox (Pro)</option>
                                </select>
                            </div>
                            <div class="tts-form-group">
                                <label class="tts-checkbox-wrapper">
                                    <input type="checkbox" class="tts-checkbox" checked>
                                    <span>Show files in Media Library</span>
                                </label>
                            </div>
                            <div class="tts-info-box">
                                <span class="dashicons dashicons-info"></span>
                                Files stored in: <code>/wp-content/uploads/text-to-speech/</code>
                            </div>
                        </div>
                    </div>

                    <!-- Sidebar -->
                    <div class="tts-sidebar">
                        <!-- Quick Stats -->
                        <div class="tts-card">
                            <h3 class="tts-card-title">
                                <span style="margin-right: 8px;">📊</span>
                                Quick Stats
                            </h3>
                            <div class="tts-stats">
                                <div class="tts-stat-item">
                                    <div class="tts-stat-icon">
                                        <span class="dashicons dashicons-microphone"></span>
                                    </div>
                                    <div>
                                        <div class="tts-stat-value"><?php echo number_format($total_characters); ?></div>
                                        <div class="tts-stat-label">Characters Used</div>
                                    </div>
                                </div>
                                <div class="tts-stat-item">
                                    <div class="tts-stat-icon">
                                        <span class="dashicons dashicons-chart-pie"></span>
                                    </div>
                                    <div>
                                        <div class="tts-stat-value"><?php echo esc_html($usage_percentage); ?>%</div>
                                        <div class="tts-stat-label">Monthly Usage</div>
                                        <div class="tts-progress-bar">
                                            <div class="tts-progress-fill" style="width: <?php echo esc_attr($usage_percentage); ?>%"></div>
                                        </div>
                                    </div>
                                </div>
                                <div class="tts-stat-item">
                                    <div class="tts-stat-icon">
                                        <span class="dashicons dashicons-media-audio"></span>
                                    </div>
                                    <div>
                                        <div class="tts-stat-value">23</div>
                                        <div class="tts-stat-label">Audio Files</div>
                                    </div>
                                </div>
                                <div class="tts-stat-item">
                                    <div class="tts-stat-icon">
                                        <span class="dashicons dashicons-admin-users"></span>
                                    </div>
                                    <div>
                                        <div class="tts-stat-value">5</div>
                                        <div class="tts-stat-label">Active Voices</div>
                                    </div>
                                </div>
                            </div>
                            <a href="#" class="tts-link" style="display: block; text-align: center; margin-top: 16px;">View Detailed Statistics →</a>
                        </div>

                        <!-- Need Help -->
                        <div class="tts-card tts-help-card">
                            <div class="tts-help-icon">?</div>
                            <h3>Need Help?</h3>
                            <p>Get started quickly with our comprehensive documentation and support resources.</p>
                            <div class="tts-help-links">
                                <a href="#" class="tts-help-link">
                                    <span class="dashicons dashicons-book"></span>
                                    Documentation
                                </a>
                                <a href="#" class="tts-help-link">
                                    <span class="dashicons dashicons-sos"></span>
                                    Get Support
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Other tabs (hidden by default) -->
            <div class="tts-tab-panel" id="analytics-panel">
                <div class="tts-empty-state">
                    <span class="dashicons dashicons-chart-area"></span>
                    <h3>Analytics Coming Soon</h3>
                    <p>Detailed analytics and insights will be available here.</p>
                </div>
            </div>

            <div class="tts-tab-panel" id="settings-panel">
                <div class="tts-empty-state">
                    <span class="dashicons dashicons-admin-generic"></span>
                    <h3>Quick Settings</h3>
                    <p>Manage your settings quickly from here.</p>
                </div>
            </div>

            <div class="tts-tab-panel" id="activity-panel">
                <div class="tts-empty-state">
                    <span class="dashicons dashicons-clock"></span>
                    <h3>Recent Activity</h3>
                    <p>Your recent audio generation activity will appear here.</p>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
/* Dashboard Container */
.tts-dashboard {
    max-width: 1200px;
    margin: 20px auto;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* Header Section */
.tts-header-section {
    background: white;
    border-radius: 16px;
    padding: 32px;
    margin-bottom: 24px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.06);
}

.tts-header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.tts-logo-section {
    display: flex;
    align-items: center;
    gap: 16px;
}

.tts-logo {
    width: 48px;
    height: 48px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-weight: 700;
    font-size: 18px;
}

.tts-title {
    margin: 0;
    font-size: 24px;
    font-weight: 600;
    color: #1a202c;
}

.tts-subtitle {
    margin: 4px 0 0;
    color: #718096;
    font-size: 14px;
}

.tts-upgrade-button {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 12px 24px;
    border-radius: 8px;
    text-decoration: none;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: opacity 0.2s;
}

.tts-upgrade-button:hover {
    opacity: 0.9;
    color: white;
}

/* Navigation Tabs */
.tts-nav-tabs {
    display: flex;
    gap: 4px;
    background: white;
    padding: 4px;
    border-radius: 12px;
    margin-bottom: 24px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.06);
}

.tts-nav-tab {
    padding: 12px 20px;
    background: none;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    color: #718096;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: all 0.2s;
}

.tts-nav-tab:hover {
    background: #f7fafc;
    color: #4a5568;
}

.tts-nav-tab.active {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.tts-nav-tab .dashicons {
    font-size: 18px;
    width: 18px;
    height: 18px;
}

/* Tab Panels */
.tts-tab-panel {
    display: none;
}

.tts-tab-panel.active {
    display: block;
}

/* Main Grid */
.tts-main-grid {
    display: grid;
    grid-template-columns: 1fr 350px;
    gap: 24px;
}

/* Cards */
.tts-card {
    background: white;
    border-radius: 16px;
    padding: 24px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.06);
    margin-bottom: 24px;
}

.tts-card-title {
    margin: 0 0 20px;
    font-size: 18px;
    font-weight: 600;
    color: #1a202c;
    display: flex;
    align-items: center;
}

/* Welcome Card */
.tts-welcome-card {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.tts-welcome-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.tts-welcome-card h3 {
    color: white;
    margin: 0 0 12px;
    font-size: 28px;
}

.tts-welcome-card p {
    color: rgba(255, 255, 255, 0.9);
    margin: 0 0 20px;
    line-height: 1.6;
}

.tts-audio-preview {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 12px;
    padding: 32px;
    text-align: center;
    min-width: 120px;
}

.tts-audio-preview .dashicons {
    font-size: 32px;
    width: 32px;
    height: 32px;
    color: white;
}

.tts-audio-preview p {
    margin: 8px 0 0;
    font-size: 12px;
}

/* Form Elements */
.tts-form-group {
    margin-bottom: 20px;
}

.tts-label {
    display: block;
    margin-bottom: 8px;
    font-weight: 500;
    color: #4a5568;
    font-size: 14px;
}

.tts-input {
    width: 100%;
    padding: 10px 16px;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    font-size: 14px;
    transition: border-color 0.2s;
}

.tts-input:focus {
    outline: none;
    border-color: #667eea;
}

.tts-input-group {
    position: relative;
    display: flex;
}

.tts-input-group .tts-input {
    padding-right: 48px;
}

.tts-input-button {
    position: absolute;
    right: 1px;
    top: 1px;
    bottom: 1px;
    padding: 0 16px;
    background: #f7fafc;
    border: none;
    border-radius: 0 7px 7px 0;
    cursor: pointer;
    display: flex;
    align-items: center;
}

.tts-input-button:hover {
    background: #e2e8f0;
}

.tts-select {
    width: 100%;
    padding: 10px 16px;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    font-size: 14px;
    background: white;
}

.tts-checkbox-wrapper {
    display: flex;
    align-items: center;
    cursor: pointer;
}

.tts-checkbox {
    margin-right: 8px;
}

/* Buttons */
.tts-button {
    padding: 10px 20px;
    border-radius: 8px;
    font-weight: 600;
    font-size: 14px;
    border: none;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: all 0.2s;
}

.tts-button-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.tts-button-primary:hover {
    opacity: 0.9;
}

.tts-button-white {
    background: white;
    color: #667eea;
}

.tts-button-white:hover {
    background: rgba(255, 255, 255, 0.9);
}

/* Status Badge */
.tts-api-status {
    display: flex;
    align-items: center;
    gap: 12px;
}

.tts-status-badge {
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 500;
}

.tts-status-badge.connected {
    background: #d4f1d4;
    color: #22543d;
}

.tts-status-badge.disconnected {
    background: #fed7d7;
    color: #742a2a;
}

/* Form Actions */
.tts-form-actions {
    display: flex;
    align-items: center;
    gap: 16px;
    margin-top: 24px;
}

/* Links */
.tts-link {
    color: #667eea;
    text-decoration: none;
    font-weight: 500;
    font-size: 14px;
}

.tts-link:hover {
    text-decoration: underline;
}

/* Info Box */
.tts-info-box {
    background: #f7fafc;
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 13px;
    color: #4a5568;
    display: flex;
    align-items: center;
    gap: 8px;
}

.tts-info-box .dashicons {
    color: #667eea;
}

/* Stats */
.tts-stats {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.tts-stat-item {
    display: flex;
    align-items: center;
    gap: 16px;
}

.tts-stat-icon {
    width: 40px;
    height: 40px;
    background: #f7fafc;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.tts-stat-icon .dashicons {
    font-size: 20px;
    color: #667eea;
}

.tts-stat-value {
    font-size: 24px;
    font-weight: 700;
    color: #1a202c;
    line-height: 1;
}

.tts-stat-label {
    font-size: 13px;
    color: #718096;
    margin-top: 4px;
}

/* Progress Bar */
.tts-progress-bar {
    height: 4px;
    background: #e2e8f0;
    border-radius: 2px;
    margin-top: 8px;
    overflow: hidden;
}

.tts-progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
    transition: width 0.3s;
}

/* Help Card */
.tts-help-card {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    color: white;
    text-align: center;
}

.tts-help-icon {
    width: 48px;
    height: 48px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 16px;
    font-size: 24px;
    font-weight: 700;
}

.tts-help-card h3 {
    color: white;
    margin: 0 0 12px;
}

.tts-help-card p {
    color: rgba(255, 255, 255, 0.9);
    margin: 0 0 20px;
    line-height: 1.6;
}

.tts-help-links {
    display: flex;
    gap: 8px;
    justify-content: center;
}

.tts-help-link {
    background: rgba(255, 255, 255, 0.2);
    color: white;
    padding: 8px 16px;
    border-radius: 8px;
    text-decoration: none;
    font-size: 13px;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    transition: background 0.2s;
}

.tts-help-link:hover {
    background: rgba(255, 255, 255, 0.3);
    color: white;
}

/* Empty State */
.tts-empty-state {
    padding: 80px 40px;
    text-align: center;
    color: #718096;
}

.tts-empty-state .dashicons {
    font-size: 48px;
    width: 48px;
    height: 48px;
    color: #cbd5e0;
    margin-bottom: 16px;
}

.tts-empty-state h3 {
    margin: 0 0 8px;
    color: #4a5568;
}

/* Responsive */
@media (max-width: 1024px) {
    .tts-main-grid {
        grid-template-columns: 1fr;
    }
    
    .tts-header-content {
        flex-direction: column;
        gap: 16px;
        align-items: start;
    }
}

@media (max-width: 768px) {
    .tts-nav-tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .tts-nav-tab {
        white-space: nowrap;
    }
    
    .tts-welcome-content {
        flex-direction: column;
        gap: 24px;
    }
}
</style>

<script>
jQuery(document).ready(function($) {
    // Tab switching
    $('.tts-nav-tab').on('click', function() {
        var tabId = $(this).data('tab');
        
        // Update active tab
        $('.tts-nav-tab').removeClass('active');
        $(this).addClass('active');
        
        // Update active panel
        $('.tts-tab-panel').removeClass('active');
        $('#' + tabId + '-panel').addClass('active');
    });
    
    // Password visibility toggle
    $('.tts-input-button').on('click', function() {
        var input = $(this).siblings('.tts-input');
        var icon = $(this).find('.dashicons');
        
        if (input.attr('type') === 'password') {
            input.attr('type', 'text');
            icon.removeClass('dashicons-visibility').addClass('dashicons-hidden');
        } else {
            input.attr('type', 'password');
            icon.removeClass('dashicons-hidden').addClass('dashicons-visibility');
        }
    });
});
</script>