<?php

namespace MovieEngine\Admin\Dashboard;

if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

class DashboardSettings {

    public static function render() {
        $analytics = Analytics::get_dashboard_data();
        $stats = $analytics['stats'];
        $period = $analytics['period'];

        $movie_count = wp_count_posts('movie_engine_movie')->publish;
        $series_count = wp_count_posts('movie_engine_series')->publish;

        $api_options = get_option('movie_engine_api_settings', []);
        $tmdb_key    = $api_options['movie_engine_tmdb_api_key'] ?? '';
        $tmdb_status = ! empty($tmdb_key);
?>
        <div class="movie-engine-settings-section">
            <div class="movie-engine-section-header me-flex-between">
                <div>
                    <h2><?php esc_html_e('Command Center', 'cine-movie-engine'); ?></h2>
                    <p class="description"><?php esc_html_e('Real-time statistics and business intelligence.', 'cine-movie-engine'); ?></p>
                </div>
                <div class="movie-engine-dashboard-actions">
                    <select id="movie-engine-dashboard-period" class="movie-engine-select me-select-period">
                        <option value="7" <?php selected($period, 7); ?>><?php esc_html_e('Last 7 Days', 'cine-movie-engine'); ?></option>
                        <option value="30" <?php selected($period, 30); ?>><?php esc_html_e('Last 30 Days', 'cine-movie-engine'); ?></option>
                        <option value="90" <?php selected($period, 90); ?>><?php esc_html_e('Last 3 Months', 'cine-movie-engine'); ?></option>
                    </select>
                </div>
            </div>

            <div class="movie-engine-dashboard-grid">
                <?php if (apply_filters('movie_engine_is_pro', false)) : ?>
                    <div class="movie-engine-stat-card">
                        <div class="movie-engine-stat-icon me-stat-revenue">
                            <span class="dashicons dashicons-chart-line"></span>
                        </div>
                        <div class="movie-engine-stat-info">
                            <h3><?php echo esc_html($stats['currency']) . esc_html(number_format_i18n($stats['revenue'], 2)); ?></h3>
                            <p><?php esc_html_e('Total Revenue', 'cine-movie-engine'); ?></p>
                        </div>
                    </div>

                    <div class="movie-engine-stat-card">
                        <div class="movie-engine-stat-icon me-stat-sales">
                            <span class="dashicons dashicons-performance"></span>
                        </div>
                        <div class="movie-engine-stat-info">
                            <h3><?php echo esc_html($stats['conversion_rate']); ?>%</h3>
                            <p><?php esc_html_e('Conversion Rate', 'cine-movie-engine'); ?></p>
                        </div>
                    </div>

                    <div class="movie-engine-stat-card">
                        <div class="movie-engine-stat-icon me-stat-subscriptions">
                            <span class="dashicons dashicons-admin-users"></span>
                        </div>
                        <div class="movie-engine-stat-info">
                            <h3><?php echo esc_html(number_format_i18n($stats['subscribers'])); ?></h3>
                            <p><?php esc_html_e('Active Subscribers', 'cine-movie-engine'); ?></p>
                        </div>
                    </div>
                <?php endif; ?>

                <div class="movie-engine-stat-card">
                    <div class="movie-engine-stat-icon me-stat-refunds">
                        <span class="dashicons dashicons-visibility"></span>
                    </div>
                    <div class="movie-engine-stat-info">
                        <h3><?php echo esc_html(number_format_i18n($stats['total_views'])); ?></h3>
                        <p><?php esc_html_e('Total Stream Views', 'cine-movie-engine'); ?></p>
                    </div>
                </div>

                <div class="movie-engine-stat-card">
                    <div class="movie-engine-stat-icon me-stat-users">
                        <span class="dashicons dashicons-format-video"></span>
                    </div>
                    <div class="movie-engine-stat-info">
                        <h3><?php echo esc_html(number_format_i18n($movie_count)); ?></h3>
                        <p><?php esc_html_e('Movies In Catalog', 'cine-movie-engine'); ?></p>
                    </div>
                </div>

                <div class="movie-engine-stat-card">
                    <div class="movie-engine-stat-icon me-stat-affiliates">
                        <span class="dashicons dashicons-playlist-video"></span>
                    </div>
                    <div class="movie-engine-stat-info">
                        <h3><?php echo esc_html(number_format_i18n($series_count)); ?></h3>
                        <p><?php esc_html_e('TV Series', 'cine-movie-engine'); ?></p>
                    </div>
                </div>

                <div class="movie-engine-stat-card">
                    <div class="movie-engine-stat-icon me-stat-content">
                        <span class="dashicons dashicons-cloud"></span>
                    </div>
                    <div class="movie-engine-stat-info">
                        <h3><?php echo $tmdb_status ? esc_html__('Active', 'cine-movie-engine') : esc_html__('Inactive', 'cine-movie-engine'); ?></h3>
                        <p><?php esc_html_e('TMDb Feed Status', 'cine-movie-engine'); ?></p>
                    </div>
                </div>
            </div>

            <div class="movie-engine-card me-mb-30">
                <div class="movie-engine-card-header me-flex-between">
                    <h3><span class="dashicons dashicons-chart-area"></span> <?php echo apply_filters('movie_engine_is_pro', false) ? esc_html__('Revenue & Growth Trends', 'cine-movie-engine') : esc_html__('Growth Trends', 'cine-movie-engine'); ?></h3>
                    <div class="movie-engine-badge me-badge-primary">
                        <?php
                        if ($period == 90) echo esc_html__('Last 3 Months', 'cine-movie-engine');
                        else printf(
                            /* translators: %d: number of days */
                            esc_html__('Last %d Days', 'cine-movie-engine'),
                            absint($period)
                        );
                        ?>
                    </div>
                </div>
                <div class="movie-engine-card-body">
                    <canvas id="movie-engine-content-chart" height="300" class="me-chart-canvas"></canvas>
                </div>
            </div>

            <div class="movie-engine-card-grid-2-col <?php echo !apply_filters('movie_engine_is_pro', false) ? 'me-pro-disabled' : ''; ?>">
                <div class="movie-engine-card">
                    <div class="movie-engine-card-header">
                        <h3><span class="dashicons dashicons-star-filled"></span> <?php esc_html_e('Top Performing Content', 'cine-movie-engine'); ?></h3>
                    </div>
                    <div class="movie-engine-card-body me-no-padding">
                        <ul class="movie-engine-list-group">
                            <?php if (! empty($analytics['top_content'])) : ?>
                                <?php foreach ($analytics['top_content'] as $content) : ?>
                                    <li class="movie-engine-list-item">
                                        <div class="movie-engine-list-content">
                                            <span class="movie-engine-list-title"><?php echo esc_html($content['title']); ?></span>
                                            <span class="movie-engine-list-meta">
                                                <?php echo esc_html(sprintf(
                                                    /* translators: %s: total number of views formatted */
                                                    esc_html__('%s total views', 'cine-movie-engine'),
                                                    number_format_i18n($content['views'])
                                                )); ?>
                                            </span>
                                        </div>
                                        <a href="<?php echo esc_url($content['link']); ?>" class="button button-small">
                                            <?php esc_html_e('View Stats', 'cine-movie-engine'); ?>
                                        </a>
                                    </li>
                                <?php endforeach; ?>
                            <?php else : ?>
                                <li class="movie-engine-list-item"><?php esc_html_e('No data available.', 'cine-movie-engine'); ?></li>
                            <?php endif; ?>
                        </ul>
                    </div>
                </div>

                <?php if (apply_filters('movie_engine_is_pro', false)) : ?>
                    <div class="movie-engine-card">
                        <div class="movie-engine-card-header">
                            <h3><span class="dashicons dashicons-money-alt"></span> <?php esc_html_e('Top Customers', 'cine-movie-engine'); ?></h3>
                        </div>
                        <div class="movie-engine-card-body me-no-padding">
                            <ul class="movie-engine-list-group">
                                <?php if (! empty($analytics['top_users'])) : ?>
                                    <?php foreach ($analytics['top_users'] as $user) : ?>
                                        <li class="movie-engine-list-item">
                                            <div class="movie-engine-list-content me-list-flex">
                                                <img src="<?php echo esc_url($user['avatar']); ?>" class="movie-engine-list-avatar" alt="">
                                                <div>
                                                    <span class="movie-engine-list-title me-list-block"><?php echo esc_html($user['name']); ?></span>
                                                    <span class="movie-engine-list-meta me-list-small">
                                                        <?php echo esc_html(sprintf(
                                                            /* translators: 1: number of orders */
                                                            _n('%1$s order', '%1$s orders', $user['count'], 'cine-movie-engine'),
                                                            $user['count']
                                                        )); ?>
                                                    </span>
                                                </div>
                                            </div>
                                            <span class="movie-engine-badge me-badge-gray me-badge-mono">
                                                <?php echo esc_html($stats['currency']) . esc_html(number_format_i18n($user['spent'], 2)); ?>
                                            </span>
                                        </li>
                                    <?php endforeach; ?>
                                <?php else : ?>
                                    <li class="movie-engine-list-item me-list-empty"><?php esc_html_e('No sales data yet.', 'cine-movie-engine'); ?></li>
                                <?php endif; ?>
                            </ul>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
            <div class="movie-engine-card me-mt-30">
                <div class="movie-engine-card-header">
                    <h3><span class="dashicons dashicons-search"></span> <?php esc_html_e('Trending Searches', 'cine-movie-engine'); ?></h3>
                </div>
                <div class="movie-engine-card-body me-no-padding">
                    <ul class="movie-engine-list-group">
                        <?php if (! empty($analytics['top_searches'])) : ?>
                            <?php foreach ($analytics['top_searches'] as $term => $count) : ?>
                                <li class="movie-engine-list-item">
                                    <div class="movie-engine-list-content">
                                        <span class="movie-engine-list-title">"<?php echo esc_html($term); ?>"</span>
                                    </div>
                                    <span class="movie-engine-badge me-badge-gray">
                                        <?php echo sprintf(
                                            /* translators: %d: number of searches */
                                            esc_html__('%d searches', 'cine-movie-engine'),
                                            absint($count)
                                        ); ?>
                                    </span>
                                </li>
                            <?php endforeach; ?>
                        <?php else : ?>
                            <li class="movie-engine-list-item"><?php esc_html_e('No search data yet.', 'cine-movie-engine'); ?></li>
                        <?php endif; ?>
                    </ul>
                </div>
            </div>
            <?php
            wp_add_inline_script('movie-engine-admin', 'window.meDashboardData = ' . json_encode(array_merge($analytics, ['is_pro' => (bool) apply_filters('movie_engine_is_pro', false)])) . ';');
            ?>
        </div>
<?php
    }
}
