<?php

namespace MovieEngine\Admin;

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

class Upgrade {

    /**
     * Get Pro URL dynamically
     */
    public static function get_pro_url($path = '') {
        $base = 'https://movieengine.pro/';
        $url = $base . ltrim($path, '/');
        return esc_url(add_query_arg([
            'utm_source' => 'orgplugin',
            'utm_medium' => 'orgadmin'
        ], $url));
    }

    /**
     * Render a premium upgrade landing page
     */
    public static function render_upgrade_page($feature_name, $description, $features_list = [], $bg_image = '') {
        $header_style = '';
        if ($bg_image) {
            $header_style = "background-image: url('{$bg_image}'); background-size: cover; background-position: center;";
        }
?>
        <div class="movie-engine-settings-wrapper me-upgrade-wrapper me-upgrade-dark-mode" style="<?php echo esc_attr($header_style); ?>">
            <div class="movie-engine-upgrade-container">
                <div class="me-upgrade-header">
                    <div class="me-lock-icon">
                        <span class="fa-solid fa-lock"></span>
                    </div>
                    <h1><?php echo esc_html($feature_name); ?> <span><?php esc_html_e('(Pro Feature)', 'cine-movie-engine'); ?></span></h1>
                    <p class="me-upgrade-subtitle">
                        <?php printf(
                            // translators: %s: feature name
                            esc_html__('Upgrade to Movie Engine Pro to import unlimited %s in one click.', 'cine-movie-engine'),
                            esc_html(strtolower($feature_name))
                        );
                        ?>
                    </p>
                </div>

                <div class="me-upgrade-content">
                    <div class="me-upgrade-features-list">
                        <?php if (empty($features_list)) {
                            $features_list = [
                                esc_html__('Import with Posters & Backdrops', 'cine-movie-engine'),
                                esc_html__('Auto Fetch Cast & Crew', 'cine-movie-engine'),
                                esc_html__('Auto Download Trailers', 'cine-movie-engine'),
                                esc_html__('Season & Episode Auto Creation', 'cine-movie-engine'),
                                esc_html__('Smart Duplicate Detection', 'cine-movie-engine'),
                                esc_html__('Faster API Processing', 'cine-movie-engine'),
                                esc_html__('Background Cron Import', 'cine-movie-engine'),
                                esc_html__('Full Metadata Sync', 'cine-movie-engine')
                            ];
                        } ?>

                        <?php foreach ($features_list as $feature) : ?>
                            <div class="me-feature-item">
                                <span class="fa-solid fa-check"></span>
                                <?php echo esc_html($feature); ?>
                            </div>
                        <?php endforeach; ?>
                    </div>

                    <div class="me-upgrade-cta-box">
                        <div class="me-price-notice"><?php esc_html_e('Pro Plans Starting at $89', 'cine-movie-engine'); ?></div>
                        <div class="me-upgrade-buttons-group">
                            <a href="<?php echo esc_url(self::get_pro_url('pricing')); ?>" target="_blank" class="movie-engine-btn me-btn-red">
                                <?php esc_html_e('Upgrade to Pro', 'cine-movie-engine'); ?>
                            </a>
                            <a href="<?php echo esc_url(self::get_pro_url('features')); ?>" target="_blank" class="movie-engine-btn me-btn-glass me-mt-3">
                                <?php esc_html_e('View Features', 'cine-movie-engine'); ?>
                            </a>
                        </div>
                    </div>
                </div>

                <div class="me-upgrade-footer">
                    <p><?php esc_html_e('Already purchased? Activate your Pro license to unlock bulk import.', 'cine-movie-engine'); ?></p>
                </div>
            </div>
        </div>
    <?php
    }

    /**
     * Render a smaller upgrade notice for metabox tabs
     */
    public static function render_metabox_upgrade_notice($feature_name, $description, $features = []) {
    ?>
        <div class="movie-engine-metabox-upgrade-notice">
            <div class="me-upgrade-icon">
                <span class="fa-solid fa-lock"></span>
            </div>
            <h3><?php echo esc_html($feature_name); ?> <span><?php esc_html_e('(Pro Feature)', 'cine-movie-engine'); ?></span></h3>
            <p><?php echo esc_html($description); ?></p>

            <?php if (!empty($features)) : ?>
                <ul class="me-upgrade-feature-check">
                    <?php foreach ($features as $feature) : ?>
                        <li><span class="fa-solid fa-check"></span> <?php echo esc_html($feature); ?></li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>

            <div class="me-upgrade-actions">
                <a href="<?php echo esc_url(self::get_pro_url('pricing')); ?>" target="_blank" class="button button-primary">
                    <?php esc_html_e('Upgrade to Pro', 'cine-movie-engine'); ?>
                </a>
                <a href="<?php echo esc_url(self::get_pro_url('features')); ?>" target="_blank" class="button me-btn-outline">
                    <?php esc_html_e('View Features', 'cine-movie-engine'); ?>
                </a>
            </div>
        </div>
<?php
    }
}
