<?php
/**
 * Renders the license activation page
 */
function atfpp_render_license_page() {
    $text_domain = 'autopoly-ai-translation-for-polylang-pro';
    $purchase_email = get_option('AIAutomaticTranslationsForPolylang_lic_email', get_bloginfo('admin_email'));
    
    // Escape early for security
    $admin_url = esc_url(admin_url('admin-post.php'));
    $purchase_email_escaped = esc_attr($purchase_email);
    ?>
    <div class="atfpp-dashboard-license">
        <div class="atfpp-dashboard-license-container">
            <div class="header">
                <h1>🔑 <?php echo esc_html__('Activate License', $text_domain); ?></h1>
            </div>
            <div class="atfpp-dashboard-license-form">
                <form method="post" action="<?php echo $admin_url; ?>">
                    <input type="hidden" name="action" value="AIAutomaticTranslationsForPolylang_el_activate_license"/>
                    <?php wp_nonce_field('el-atfpp-license'); ?>
                    
                    <div class="atfpp-dashboard-license-field">
                        <label for="license_code"><?php esc_html_e('License Key', $text_domain); ?></label>
                        <input type="text" name="license_code" id="license_code" required 
                            placeholder="<?php esc_attr_e('xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxx', $text_domain); ?>">
                    </div>
                    
                    <div class="atfpp-dashboard-license-field">
                        <label for="email"><?php esc_html_e('Email Address', $text_domain); ?></label>
                        <input type="email" name="email" id="email" required 
                            value="<?php echo $purchase_email_escaped; ?>">
                        <small class="atfpp-dashboard-activation-note"><?php esc_html_e("Plugin updates news will be sent to this email. Don't worry, we hate spam.", $text_domain); ?></small>
                    </div>
                    
                    <button type="submit" class="button button-primary">
                        <?php echo esc_html__('Activate License', $text_domain); ?>
                    </button>
                </form>

                <p class="activation-note">
                    <?php echo esc_html__('Activate to receive automatic plugin updates and support.', $text_domain); ?>
                </p>

                <?php atfpp_render_license_help_buttons($text_domain); ?>
            </div>
        </div>
    </div>
<?php
}

/**
 * Renders the license information page for Pro users
 * 
 * @param object|null $license_info License information object
 */
function atfpp_render_license_page_pro($license_info = null) {
    $text_domain = 'autopoly-ai-translation-for-polylang-pro';
    
    // Early return if invalid license info
    if (!$license_info) {
        $license_info = AI_Automatic_Translations_For_Polylang_Base::GetRegisterInfo();
    }

    if (!is_object($license_info) || !isset($license_info->is_valid) || !isset($license_info->license_title) || !isset($license_info->expire_date)) {
        wp_die(__('Error: Invalid license information', $text_domain));
        return;
    }
    
    // Sanitize license key before masking
    $license_key = sanitize_text_field(get_option('AIAutomaticTranslationsForPolylang_lic_Key', ''));
    $masked_key = !empty($license_key) ? 
        esc_html(substr($license_key, 0, 8) . '-XXXXXXXX-XXXXXXXX-' . substr($license_key, -8)) :
        '';
    
    $admin_url = esc_url(admin_url('admin-post.php'));
?>
    <div class="atfpp-dashboard-license">
        <div class="atfpp-dashboard-license-pro-container">
            <div class="atfpp-dashboard-license-header-container" style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
                <h1>🔒 <?php esc_html_e('Your License Info', $text_domain); ?></h1>
                <?php if (atfpp_needs_refresh($license_info)): ?>

                    <button type="button" class="atfpp-refresh-btn" id="atfpp-refresh-license-btn">
                        <?php esc_html_e('🔄Check license status', $text_domain); ?>

                    </button>
                <?php endif; ?>
            </div>
            <ul>
                <li><strong><?php esc_html_e('Status:', $text_domain); ?></strong> 
                    <span class="validity">
                        <?php if ($license_info->is_valid): ?>

                            <?php if (atfpp_is_license_expired($license_info)): ?>

                                <strong>❌ <?php esc_html_e('License Expired', $text_domain); ?></strong>

                            <?php elseif (atfpp_is_support_expired($license_info)): ?>

                                <strong>❌ <?php esc_html_e('Support Expired', $text_domain); ?></strong>

                            <?php else: ?>

                                <strong class="valid">✅ <?php esc_html_e('Valid', $text_domain); ?></strong>

                            <?php endif; ?>

                        <?php else: ?>

                        <strong>❌ <?php esc_html_e('Invalid', $text_domain); ?></strong>
                        
                        <?php endif; ?>
                    </span>
                </li>
                <li><strong><?php esc_html_e('License Type:', $text_domain); ?></strong> <span class="license-type"><?php echo esc_html($license_info->license_title); ?></span></li>
                <li><strong><?php esc_html_e('Plugin Updates & Support Validity:', $text_domain); ?></strong> <span class="validity">

                    <?php 
                    $current_time = time();
                 
                    // Handle "No expiry" case for expire_date
                    $expire_date_expired = false;
                    if (strtolower($license_info->expire_date) !== 'no expiry') {
                        $expire_date_timestamp = strtotime($license_info->expire_date);
                        $expire_date_expired = $expire_date_timestamp && $expire_date_timestamp < $current_time;
                    }
                    
                    // Handle "no support" case for support_end
                    if (strtolower($license_info->support_end) === 'no support') {

                        esc_html_e('No Support', $text_domain);

                    } else {

                        // Handle "unlimited" case for support_end
                        $support_end_expired = false;

                        if (strtolower($license_info->support_end) !== 'unlimited') {

                            $support_end_timestamp = strtotime($license_info->support_end);
                            $support_end_expired = $support_end_timestamp && $support_end_timestamp < $current_time;

                        }
                        if ($expire_date_expired) {
                            
                            echo esc_html(atfpp_pro_formatLicenseDate($license_info->expire_date));

                        } elseif ($support_end_expired) {                            
                            
                            echo esc_html(atfpp_pro_formatLicenseDate($license_info->support_end));
                          
                        } else {

                            echo esc_html(atfpp_pro_formatLicenseDate($license_info->expire_date));
                            
                        }
                       
                    }
                    ?>
                    </span>
                </li>

                <li><strong><?php echo esc_html__('Your License Key:', $text_domain); ?></strong> <span class="license-key"><?php echo esc_html($masked_key); ?></span></li>
            </ul>

            <div class="atfpp-dashboard-license-pro-container-deactivate-btn">
                <p><?php esc_html_e('Want to deactivate the license for any reason?', $text_domain); ?></p>
                <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
                    <input type="hidden" name="action" value="AIAutomaticTranslationsForPolylang_el_deactivate_license" />
                    <?php wp_nonce_field('el-atfpp-license'); ?>
                    <button type="submit" class="deactivate-btn">
                        <?php echo esc_html__('Deactivate License', $text_domain); ?>
                    </button>
                </form>
            </div>
            <?php if (atfpp_is_license_expired($license_info)): ?>

            <div class="notice notice-error" style="margin-top: 10px; color: #d63638;">
                <?php atfpp_render_expiry_message($license_info, 'license'); ?>
            </div>

            <?php elseif (atfpp_is_support_expired($license_info)): ?>

            <div class="notice notice-error" style="margin-top: 10px; color: #d63638;">
                <?php atfpp_render_expiry_message($license_info, 'support'); ?>
            </div>

            <?php endif; ?>

            <?php atfpp_render_license_help_buttons($text_domain); ?>
        </div>
    </div>
<?php
}

/**
 * Renders the license buttons section
 * @param string $text_domain The text domain for translations
 */
function atfpp_render_license_help_buttons($text_domain) {
    ?>
    <div class="atfpp-dashboard-license-pro-container-buttons">
        <p><?php esc_html_e('Want to know more about the license key?', $text_domain); ?></p>
        <div class="btns">
            <a href="https://my.coolplugins.net/account/" target="_blank" class="atfpp-dashboard-btn">
                <?php echo esc_html__('Check Account', $text_domain); ?>
            </a>
            <a href="https://coolplugins.net/support/?utm_source=atfp_plugin&utm_medium=inside&utm_campaign=support&utm_content=dashboard_license" target="_blank" class="atfpp-dashboard-btn">
                <?php echo esc_html__('Contact Support', $text_domain); ?>
            </a>
        </div>
    </div>
    <?php
}

function atfpp_is_license_expired($license_info) {

    return $license_info->is_valid === 'license_expired';
}

function atfpp_is_support_expired($license_info) {

    return $license_info->support_end === 'no support' || 
          ($license_info->is_valid === 'support_expired' || 
          (strtolower($license_info->support_end) !== 'unlimited' && 
          strtotime($license_info->support_end) < time()));
}

function atfpp_needs_refresh($license_info) {

    return atfpp_is_license_expired($license_info) || atfpp_is_support_expired($license_info);
}


function atfpp_render_expiry_message($license_info, $type = 'license') {
 
    $text_domain = 'autopoly-ai-translation-for-polylang-pro';
    
    // Generate version available message using common helper
    $version_available_message = AutoPolyPro::atfppGetVersionAvailableMessage();
    
    if ($license_info->msg === 'limit_reached') {
        $support_link = sprintf('<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>', esc_url('https://my.coolplugins.net/account/support-tickets/'), esc_html__('clicking here', 'atfpp'));
        echo wp_kses_post(sprintf(
            /* translators: %s: link to support ticket page */
            __('There was an issue with your account. Please contact our plugin support team by %s.', 'atfpp'),
            $support_link
        ));
        return;
    }

    $message = $type === 'license' 
        ? __('Your license has expired,', 'atfpp') 
        : __('Your support has expired,', 'atfpp');

    $renew_link = isset($license_info->market) && $license_info->market === 'E'
        ? ''
        : ' <a href="'.esc_url('https://my.coolplugins.net/account/subscriptions/').'" target="_blank" rel="noopener noreferrer">'.esc_html__('Renew now', 'atfpp').'</a>';

     $final_message = '';
    
    // Add version message if available
    if (!empty($version_available_message)) {

        wp_enqueue_script('thickbox');
        wp_enqueue_style('thickbox');
        
        $final_message .= wp_kses_post($version_available_message) . ' ';
    }
    
    // Add license expiry message
    $final_message .= esc_html($message) . $renew_link . esc_html__(' to continue receiving updates and priority support.', 'atfpp');
    
    echo $final_message;
}

function atfpp_pro_formatLicenseDate($dateString) {

if (!empty($dateString) && strtolower($dateString) !== 'no expiry') {
     
        $date = new DateTime($dateString);
        return $date->format('d M Y');
    }
    return $dateString;
}

