<?php

namespace MovieEngine\Admin\Home;

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

class Welcome {

    public static function render() {
        $themes = [
            [
                'title'       => esc_html__('Hello Movie Engine', 'cine-movie-engine'),
                'slug'        => 'hello-movieengine',
                'description' => esc_html__('Hello Movie Engine is Official Theme for Movie Engine Plugin.', 'cine-movie-engine'),
                'thumbnail'   => 'https://raw.githubusercontent.com/themepuls/hello-movieengine/refs/heads/main/screenshot.png',
                'type'        => 'free',
                'badge'       => 'Official',
                'price'       => 'Free',
                'demo'        => '',
                'url'         => '',
                'github_zip'  => 'https://github.com/themepuls/hello-movieengine/archive/refs/heads/main.zip',
                'author'      => 'MovieEngine',
                'rating'      => '4.9',
                'is_official' => true,
            ]

            // [
            //     'title'       => esc_html__('Hello Elementor', 'cine-movie-engine'),
            //     'slug'        => 'hello-elementor',
            //     'description' => esc_html__('A lightweight starter theme, perfect for the Elementor page builder.', 'cine-movie-engine'),
            //     'thumbnail'   => '', // Logic below will auto-fetch from WP.org
            //     'type'        => 'free',
            //     'badge'       => 'WP.org',
            //     'price'       => 'Free',
            //     'demo'        => 'https://elementor.com/hello-theme/',
            //     'url'         => 'https://wordpress.org/themes/hello-elementor/',
            //     'github_zip'  => '',
            //     'author'      => 'Elementor',
            //     'rating'      => '4.5',
            //     'is_official' => true,
            // ]
        ];

        // Process theme status
        foreach ($themes as &$theme) {
            $theme['is_installed'] = false;
            $theme['is_active']    = false;

            // Auto-fetch screenshot for WP.org themes
            if (empty($theme['github_zip']) && empty($theme['thumbnail']) && !empty($theme['slug'])) {
                $theme['thumbnail'] = "https://ts.w.org/wp-content/themes/{$theme['slug']}/screenshot.png";
            }

            if (! empty($theme['slug'])) {
                $theme_info = wp_get_theme($theme['slug']);
                if ($theme_info->exists()) {
                    $theme['is_installed'] = true;
                    if (get_stylesheet() === $theme['slug'] || get_template() === $theme['slug']) {
                        $theme['is_active'] = true;
                    }
                }
            }
        }
        unset($theme);

        $features = [
            ['icon' => 'fa-bolt',           'title' => 'Fast Importer',   'desc' => 'Import 100+ movies in seconds with TMDB API.'],
            ['icon' => 'fa-play-circle',    'title' => 'Multi-Player',    'desc' => 'Supports Plyr, Clappr, and Shaka player for all devices.'],
            ['icon' => 'fa-lock',           'title' => 'Paywall Engine',  'desc' => 'Easily restrict content and sell premium subscriptions.'],
            ['icon' => 'fa-mobile-alt',     'title' => 'Mobile Ready',    'desc' => 'Fully responsive and works perfect on the MovieEngine App.'],
        ];

        // Live site stats
        $movie_count  = (int) wp_count_posts('movie_engine_movie')->publish;
        $series_count = (int) wp_count_posts('movie_engine_series')->publish;
        $user_count   = (int) count_users()['total_users'];
        $genre_count  = (int) wp_count_terms(['taxonomy' => 'movie_engine_genre', 'hide_empty' => false]);

        // Quick action shortcuts
        $quick_actions = [
            [
                'icon'  => 'fa-file-import',
                'label' => __('Import Movies', 'cine-movie-engine'),
                'url'   => admin_url('admin.php?page=movie-engine-import'),
                'color' => 'blue',
            ],
            [
                'icon'  => 'fa-film',
                'label' => __('Add Movie', 'cine-movie-engine'),
                'url'   => admin_url('post-new.php?post_type=movie_engine_movie'),
                'color' => 'red',
            ],
            [
                'icon'  => 'fa-television',
                'label' => __('Add Series', 'cine-movie-engine'),
                'url'   => admin_url('post-new.php?post_type=movie_engine_series'),
                'color' => 'purple',
            ],
            [
                'icon'  => 'fa-users',
                'label' => __('Manage Users', 'cine-movie-engine'),
                'url'   => admin_url('users.php'),
                'color' => 'green',
            ],
            [
                'icon'  => 'fa-sliders',
                'label' => __('Settings', 'cine-movie-engine'),
                'url'   => admin_url('admin.php?page=movie-engine-settings'),
                'color' => 'orange',
            ],
            [
                'icon'  => 'fa-book-open',
                'label' => __('Documentation', 'cine-movie-engine'),
                'url'   => 'https://docs.movieengine.pro/',
                'color' => 'slate',
            ],
        ];
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $paged          = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
        $per_page       = 14;
        $total          = count($themes);
        $pages          = ceil($total / $per_page);
        $offset         = ($paged - 1) * $per_page;
        $display_themes = array_slice($themes, $offset, $per_page);
?>
        <div class="movie-engine-welcome-wrap">
            <div class="me-welcome-hero">
                <div class="me-hero-backdrop"></div>
                <div class="me-welcome-hero-content">
                    <span class="me-hero-tag"><?php echo esc_html(MOVIE_ENGINE_VERSION); ?> <?php esc_html_e('Now Live', 'cine-movie-engine'); ?></span>
                    <h1><?php esc_html_e('Next-Gen Movie Platform', 'cine-movie-engine'); ?></h1>
                    <p><?php esc_html_e('Build your own streaming empire with MovieEngine. A professional, scalable, and feature-rich solution for WordPress authors.', 'cine-movie-engine'); ?></p>
                    <div class="me-welcome-hero-actions">
                        <a href="<?php echo esc_url(admin_url('admin.php?page=movie-engine-setup')); ?>" class="me-btn me-btn-primary">
                            <i class="fa-solid fa-wand-magic-sparkles"></i> <?php esc_html_e('Set Up Now', 'cine-movie-engine'); ?>
                        </a>
                        <a href="https://docs.movieengine.pro/" target="_blank" class="me-btn me-btn-glass">
                            <i class="fa-solid fa-book-open"></i> <?php esc_html_e('Explore Docs', 'cine-movie-engine'); ?>
                        </a>
                    </div>
                </div>
                <div class="me-welcome-hero-stats">
                    <div class="me-welcome-stat">
                        <div class="me-stat-val"><?php echo esc_html($movie_count); ?></div>
                        <div class="me-stat-label"><?php esc_html_e('Movies', 'cine-movie-engine'); ?></div>
                    </div>
                    <div class="me-welcome-stat">
                        <div class="me-stat-val"><?php echo esc_html($series_count); ?></div>
                        <div class="me-stat-label"><?php esc_html_e('Series', 'cine-movie-engine'); ?></div>
                    </div>
                    <div class="me-welcome-stat">
                        <div class="me-stat-val"><?php echo esc_html($user_count); ?></div>
                        <div class="me-stat-label"><?php esc_html_e('Users', 'cine-movie-engine'); ?></div>
                    </div>
                    <div class="me-welcome-stat">
                        <div class="me-stat-val"><?php echo esc_html(is_wp_error($genre_count) ? 0 : $genre_count); ?></div>
                        <div class="me-stat-label"><?php esc_html_e('Genres', 'cine-movie-engine'); ?></div>
                    </div>
                </div>
            </div>
            <div class="me-quick-actions">
                <?php foreach ($quick_actions as $action) : ?>
                    <a href="<?php echo esc_url($action['url']); ?>" class="me-quick-action-card me-qa-<?php echo esc_attr($action['color']); ?>">
                        <div class="me-qa-icon"><i class="fa-solid <?php echo esc_attr($action['icon']); ?>"></i></div>
                        <span><?php echo esc_html($action['label']); ?></span>
                    </a>
                <?php endforeach; ?>
            </div>

            <div class="me-features-grid">
                <?php foreach ($features as $feat) : ?>
                    <div class="me-feature-card">
                        <div class="me-feature-icon"><i class="fa-solid <?php echo esc_attr($feat['icon']); ?>"></i></div>
                        <div class="me-feature-info">
                            <h4><?php echo esc_html($feat['title']); ?></h4>
                            <p><?php echo esc_html($feat['desc']); ?></p>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>

            <div class="me-welcome-section me-text-left me-py-1">
                <div class="me-section-header me-flex-center me-py-1">
                    <div>
                        <h2><?php esc_html_e('Theme Marketplace', 'cine-movie-engine'); ?></h2>
                        <p><?php esc_html_e('Exclusive themes and child themes optimized for maximum speed and SEO.', 'cine-movie-engine'); ?></p>
                    </div>
                    <!-- <a href="https://movieengine.pro/themes/" target="_blank" class="me-btn me-btn-outline me-btn-sm">
                        <i class="fa-solid fa-arrow-up-right-from-square"></i>
                        <?php esc_html_e('Browse All', 'cine-movie-engine'); ?>
                    </a> -->
                </div>

                <div class="me-theme-grid">
                    <?php if (! empty($display_themes)) : ?>
                        <?php foreach ($display_themes as $theme) : ?>
                            <div class="me-theme-card <?php echo esc_attr(strtolower($theme['type'])); ?>">
                                <div class="me-theme-thumb">
                                    <img src="<?php echo esc_url($theme['thumbnail']); ?>" alt="<?php echo esc_attr($theme['title']); ?>">
                                    <div class="me-theme-overlay">
                                        <div class="me-overlay-content">
                                            <a href="<?php echo esc_url($theme['demo']); ?>" target="_blank" class="me-btn me-btn-white me-btn-sm"><?php esc_html_e('Live Preview', 'cine-movie-engine'); ?></a>
                                        </div>
                                    </div>
                                    <span class="me-theme-badge-float"><?php echo esc_html($theme['badge']); ?></span>
                                </div>
                                <div class="me-theme-meta">
                                    <div class="me-theme-header">
                                        <h3><a href="<?php echo esc_url($theme['url']); ?>"><?php echo esc_html($theme['title']); ?></a></h3>
                                        <div class="me-theme-stars">
                                            <i class="fa-solid fa-star"></i> <?php echo esc_html($theme['rating']); ?>
                                        </div>
                                    </div>
                                    <p><?php echo esc_html($theme['description']); ?></p>
                                    <div class="me-theme-footer">
                                        <div class="me-author-info">
                                            <span class="me-theme-label"><?php esc_html_e('By', 'cine-movie-engine'); ?></span>
                                            <strong><?php echo esc_html($theme['author']); ?></strong>
                                        </div>
                                        <div class="me-price-action">
                                            <?php if (! empty($theme['is_official'])) : ?>
                                                <?php if ($theme['is_active']) : ?>
                                                    <span class="me-btn me-btn-success me-btn-sm disabled">
                                                        <i class="fa-solid fa-check"></i> <?php esc_html_e('Active', 'cine-movie-engine'); ?>
                                                    </span>
                                                <?php else : ?>
                                                    <button class="me-btn me-btn-primary me-btn-sm me-install-theme"
                                                        data-slug="<?php echo esc_attr($theme['slug']); ?>"
                                                        data-source="<?php echo esc_url($theme['github_zip']); ?>"
                                                        data-nonce="<?php echo esc_attr(wp_create_nonce('me_install_theme_' . $theme['slug'])); ?>">
                                                        <i class="fa-solid fa-download"></i>
                                                        <?php echo $theme['is_installed'] ? esc_html__('Activate', 'cine-movie-engine') : esc_html__('Install & Activate', 'cine-movie-engine'); ?>
                                                    </button>
                                                <?php endif; ?>
                                            <?php else : ?>
                                                <span class="me-price-tag"><?php echo esc_html($theme['price']); ?></span>
                                                <a href="<?php echo esc_url($theme['url']); ?>" target="_blank" class="me-theme-buy-btn"><i class="fa-solid fa-cart-shopping"></i></a>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>

                <?php if ($pages > 1) : ?>
                    <div class="me-pagination">
                        <?php if ($paged > 1) : ?>
                            <a href="<?php echo esc_url(add_query_arg('paged', $paged - 1)); ?>" class="me-pagination-item prev">
                                <i class="fa-solid fa-chevron-left"></i>
                            </a>
                        <?php endif; ?>

                        <?php for ($i = 1; $i <= $pages; $i++) : ?>
                            <a href="<?php echo esc_url(add_query_arg('paged', $i)); ?>" class="me-pagination-item <?php echo $i === $paged ? 'active' : ''; ?>">
                                <?php echo esc_html($i); ?>
                            </a>
                        <?php endfor; ?>

                        <?php if ($paged < $pages) : ?>
                            <a href="<?php echo esc_url(add_query_arg('paged', $paged + 1)); ?>" class="me-pagination-item next">
                                <i class="fa-solid fa-chevron-right"></i>
                            </a>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
            </div>
            <div class="me-welcome-support">
                <div class="me-support-content">
                    <h2><?php esc_html_e('Need Customization or Help?', 'cine-movie-engine'); ?></h2>
                    <p><?php esc_html_e('Our professional team is available to help you set up your streaming site or customize any features you need.', 'cine-movie-engine'); ?></p>
                    <div class="me-support-actions">
                        <a href="https://www.facebook.com/groups/movieengine" target="_blank" class="me-btn me-btn-dark">
                            <i class="fa-brands fa-facebook"></i>
                            <?php esc_html_e('Join our Facebook Community', 'cine-movie-engine'); ?>
                        </a>
                        <a href="https://movieengine.pro/hire-expert/" target="_blank" class="me-btn me-btn-outline">
                            <i class="fa-solid fa-handshake"></i>
                            <?php esc_html_e('Hire an Expert', 'cine-movie-engine'); ?>
                        </a>
                    </div>
                </div>
            </div>

        </div>
<?php
    }
}
