<?php
/**
 * View: License & Limits Page (Premium UI)
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$pexemail_tz = new DateTimeZone('Asia/Dhaka');

$pexemail_license_key   = get_option( 'pexemail_license_key', '' );
$pexemail_input_email   = get_option( 'pexemail_input_email', '' ); // Changed to blank for new installs
$pexemail_status        = get_option( 'pexemail_license_status', 'invalid' );
$pexemail_details       = get_option( 'pexemail_license_details', [] );
$pexemail_api_error_msg = get_option( 'pexemail_license_error', '' );

$pexemail_licensed_email = isset( $pexemail_details['licensed_email'] ) ? $pexemail_details['licensed_email'] : 'N/A';
$pexemail_expires_at     = isset( $pexemail_details['expires_at'] ) ? intval( $pexemail_details['expires_at'] ) : 0;
$pexemail_last_checked   = isset( $pexemail_details['last_checked'] ) ? intval( $pexemail_details['last_checked'] ) : 0;

if ( $pexemail_status === 'active' && $pexemail_expires_at > 0 && time() > $pexemail_expires_at ) {
    $pexemail_status = 'expired';
}

$pexemail_days_left = $pexemail_expires_at > 0 ? ceil( ( $pexemail_expires_at - time() ) / 86400 ) : 0;

$pexemail_plan_duration = '';
if ( $pexemail_expires_at > 0 && $pexemail_last_checked > 0 ) {
    $pexemail_total_days = round( ( $pexemail_expires_at - $pexemail_last_checked ) / 86400 );
    $pexemail_plan_duration = ( $pexemail_total_days > 200 ) ? '12 Month' : '6 Month';
}

$pexemail_badge_color = '#dc2626';
$pexemail_badge_bg    = '#fef2f2';
$pexemail_badge_text  = esc_html__( 'Invalid / Blocked', 'pexally-email-campaigns' );
$pexemail_show_renew  = false;

if ( $pexemail_status === 'active' ) {
    if ( $pexemail_expires_at > 0 && $pexemail_days_left <= 15 ) {
        $pexemail_badge_color = '#d97706';
        $pexemail_badge_bg    = '#fffbeb';
        $pexemail_badge_text  = sprintf( esc_html__( 'Active (Expires in %d Days)', 'pexally-email-campaigns' ), (int) $pexemail_days_left );
        $pexemail_show_renew  = true;
    } else {
        $pexemail_badge_color = '#10b981';
        $pexemail_badge_bg    = '#ecfdf5';
        $pexemail_badge_text  = esc_html__( 'Active', 'pexally-email-campaigns' );
    }
} elseif ( $pexemail_status === 'expired' ) {
    $pexemail_badge_color = '#991b1b';
    $pexemail_badge_bg    = '#fef2f2';
    $pexemail_badge_text  = esc_html__( 'License Expired', 'pexally-email-campaigns' );
    $pexemail_show_renew  = true;
}

$pexemail_last_checked_str = $pexemail_last_checked ? wp_date( 'd M Y, h:i A', $pexemail_last_checked, $pexemail_tz ) : 'N/A';
$pexemail_expires_at_str   = $pexemail_expires_at ? wp_date( 'd M Y, h:i A', $pexemail_expires_at, $pexemail_tz ) : 'N/A';
?>

<style>
    .pex-premium-wrap { max-width: 1400px; margin: 25px 0 0 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; color: #334155; }
    .pex-hero { background: linear-gradient(135deg, #0f5132 0%, #064e3b 100%); color: #fff; padding: 45px 50px; border-radius: 16px; margin-bottom: 35px; box-shadow: 0 15px 35px -5px rgba(6, 78, 59, 0.3), inset 0 1px 0 rgba(255,255,255,0.1); position: relative; overflow: hidden; }
    .pex-hero::after { content: ''; position: absolute; top: 0; right: 0; bottom: 0; left: 0; background: url('data:image/svg+xml;utf8,<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="40" fill="rgba(255,255,255,0.03)"/></svg>') repeat; opacity: 0.8; pointer-events: none; }
    .pex-hero-text { position: relative; z-index: 2; }
    .pex-hero h1 { color: #fff; margin: 0 0 10px 0; font-size: 30px; font-weight: 800; letter-spacing: -0.5px; text-shadow: 0 2px 4px rgba(0,0,0,0.1); }
    .pex-hero p { margin: 0; font-size: 16px; color: #d1fae5; line-height: 1.6; max-width: 650px; font-weight: 400; }
    .pex-grid { display: grid; grid-template-columns: 1fr 1.2fr; gap: 30px; }
    @media (max-width: 950px) { .pex-grid { grid-template-columns: 1fr; } }
    .pex-card { background: #ffffff; border-radius: 16px; border: 1px solid #eef2f6; box-shadow: 0 4px 6px -1px rgba(15, 23, 42, 0.02), 0 2px 4px -1px rgba(15, 23, 42, 0.02); overflow: hidden; margin-bottom: 35px; transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); display: flex; flex-direction: column; }
    .pex-card:hover { box-shadow: 0 10px 20px -5px rgba(15, 23, 42, 0.04); border-color: #e2e8f0; transform: translateY(-2px); }
    .pex-card-title { margin: 0 0 25px 0; font-size: 18px; font-weight: 800; color: #0f172a; border-bottom: 1px solid #f1f5f9; padding-bottom: 20px; display: flex; justify-content: space-between; align-items: center; letter-spacing: -0.3px; }
    .pex-card-body { padding: 35px 30px; flex-grow: 1; }
    .pex-form-label { display: block; font-size: 13.5px; font-weight: 700; color: #1e293b; margin-bottom: 10px; text-transform: uppercase; letter-spacing: 0.5px; }
    .pex-input { width: 100%; padding: 14px 18px; border: 1px solid #cbd5e1; border-radius: 10px; font-size: 15px; outline: none; transition: all 0.2s; background: #f8fafc; color: #0f172a; box-shadow: inset 0 1px 2px rgba(0,0,0,0.01); box-sizing: border-box; }
    .pex-input:focus { border-color: #10b981; background: #ffffff; box-shadow: 0 0 0 4px rgba(16, 185, 129, 0.15); }
    .pex-btn-primary { background: #0f172a; color: white; padding: 14px 28px; border-radius: 10px; font-weight: 700; font-size: 15px; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; transition: all 0.2s; border: none; cursor: pointer; box-shadow: 0 4px 6px rgba(15, 23, 42, 0.1); gap: 10px; width: 100%; box-sizing: border-box; }
    .pex-btn-primary:hover { background: #1e293b; transform: translateY(-2px); box-shadow: 0 8px 15px rgba(15, 23, 42, 0.15); color: white; }
    .pex-table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 14.5px; margin-top: 35px; background: #f8fafc; border-radius: 12px; border: 1px solid #eef2f6; overflow: hidden; }
    .pex-table td { padding: 16px 20px; border-bottom: 1px solid #eef2f6; color: #334155; }
    .pex-table td:first-child { font-weight: 700; color: #475569; width: 35%; background: #ffffff; }
    .pex-table tr:last-child td { border-bottom: none; }
    .pex-list { list-style: none; padding: 0; margin: 0; }
    .pex-list li { margin-bottom: 16px; font-size: 14.5px; color: #334155; display: flex; align-items: flex-start; gap: 12px; line-height: 1.6; padding: 8px 0; }
    .pex-list-icon { color: #10b981; font-weight: bold; font-size: 18px; margin-top: -2px; }
    .pex-highlight-block { background: linear-gradient(to right, #ecfdf5, #f0fdf4); border: 1px solid #a7f3d0; border-radius: 12px; padding: 16px 20px !important; margin-bottom: 16px !important; box-shadow: 0 4px 6px -1px rgba(16, 185, 129, 0.05); position: relative; overflow: hidden; }
    .pex-highlight-block::before { content: ''; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: #10b981; }
    .pex-warning-bar { background: linear-gradient(to right, #fff1f2, #ffe4e6); border-left: 4px solid #e11d48; padding: 20px; border-radius: 12px; display: flex; align-items: flex-start; gap: 14px; margin-top: 25px; box-shadow: 0 2px 4px rgba(225, 29, 72, 0.05); }
    .pex-warning-bar .icon { font-size: 24px; line-height: 1; margin-top: 2px; }
    .pex-warning-bar h4 { margin: 0 0 6px 0; color: #be123c; font-size: 13.5px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.5px; }
    .pex-warning-bar p { margin: 0; color: #9f1239; font-size: 14.5px; line-height: 1.5; }
    .pex-contact-note { margin-top: 25px; text-align: center; font-size: 13.5px; color: #64748b; background: #f8fafc; padding: 20px; border-radius: 12px; border: 1px dashed #cbd5e1; }
    .pex-contact-note a { color: #2563eb; font-weight: 700; text-decoration: none; transition: color 0.2s; }
    .pex-contact-note a:hover { color: #1d4ed8; text-decoration: underline; }
    .pex-custom-notice { background: #f0fdf4; border: 1px solid #bbf7d0; border-left: 4px solid #10b981; padding: 18px 20px; border-radius: 12px; color: #065f46; font-weight: 600; display: flex; align-items: center; gap: 14px; margin-bottom: 30px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.05); font-size: 15px; }
    .pex-error-notice { background: #fef2f2; border: 1px solid #fecaca; border-left: 4px solid #ef4444; padding: 18px 20px; border-radius: 12px; color: #991b1b; display: flex; align-items: flex-start; gap: 14px; margin-bottom: 30px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.05); }
    .pex-warning-notice { background-color: #fffbeb; border: 1px solid #fde68a; border-left: 4px solid #d97706; padding: 18px 20px; margin-bottom: 30px; border-radius: 12px; color: #b45309; font-weight: 600; font-size: 15px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.05); display: flex; align-items: center; gap: 14px; }
</style>

<div class="wrap pex-premium-wrap">

    <?php 
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    if ( isset( $_GET['license_updated'] ) ) : ?>
        <?php if ( $pexemail_status === 'active' ) : ?>
            <div class="pex-custom-notice">
                <span style="font-size: 22px;">✅</span>
                <?php echo esc_html( sprintf( __( '%s License is successfully activated.', 'pexally-email-campaigns' ), $pexemail_plan_duration ) ); ?>
            </div>
        <?php else : ?>
            <div class="pex-error-notice">
                <span style="font-size: 22px; margin-top: 2px;">❌</span>
                <div>
                    <strong style="display:block; margin-bottom: 6px; font-size: 15px;">License key is not valid.</strong>
                    <span style="font-size: 14px; color: #b91c1c;"><?php echo esc_html( $pexemail_api_error_msg ? $pexemail_api_error_msg : 'Please check your key and email, and try again.' ); ?></span>
                </div>
            </div>
        <?php endif; ?>
    <?php endif; ?>

    <?php if ( $pexemail_status === 'expired' ) : ?>
        <div class="pex-error-notice" style="align-items: center;">
            <span style="font-size: 24px;">🚨</span>
            <?php esc_html_e( 'Your Pexally Pro License has expired. Your daily sending limit has been reduced to 50 emails/day.', 'pexally-email-campaigns' ); ?>
        </div>
    <?php elseif ( $pexemail_status === 'active' && $pexemail_show_renew ) : ?>
        <div class="pex-warning-notice">
            <span style="font-size: 24px;">⚠️</span>
            <?php printf( esc_html__( 'Your Pexally Pro License is expiring in %d days. Please renew to avoid service interruption.', 'pexally-email-campaigns' ), (int) $pexemail_days_left ); ?>
        </div>
    <?php endif; ?>

    <div class="pex-hero">
        <div class="pex-hero-text">
            <h1><?php esc_html_e( 'Pexally Pro License', 'pexally-email-campaigns' ); ?></h1>
            <p><?php esc_html_e( 'Unlock high-volume sending and guaranteed inbox delivery by activating your premium license key.', 'pexally-email-campaigns' ); ?></p>
        </div>
    </div>

    <div class="pex-grid">
        
        <div class="pex-card">
            <div class="pex-card-body">
                <h2 class="pex-card-title"><?php esc_html_e( 'License Configuration', 'pexally-email-campaigns' ); ?></h2>
                    
                <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
                    <?php wp_nonce_field( 'pexemail_save_license_action', 'pexemail_license_nonce' ); ?>
                    <input type="hidden" name="action" value="pexemail_save_license">
                    
                    <div style="margin-bottom: 25px;">
                        <label for="pexemail_licensed_email" class="pex-form-label">
                            <?php esc_html_e( 'Licensed Email Address', 'pexally-email-campaigns' ); ?>
                        </label>
                        <input type="email" id="pexemail_licensed_email" name="pexemail_licensed_email" class="pex-input" value="<?php echo esc_attr( $pexemail_input_email ); ?>" placeholder="e.g. you@domain.com" required>
                    </div>

                    <div style="margin-bottom: 35px;">
                        <label for="pexemail_license_key" class="pex-form-label">
                            <?php esc_html_e( 'License Key', 'pexally-email-campaigns' ); ?>
                        </label>
                        <input type="text" id="pexemail_license_key" name="pexemail_license_key" class="pex-input" value="<?php echo esc_attr( $pexemail_license_key ); ?>" placeholder="e.g. PEX-XXXX-XXXX-XXXX" required>
                    </div>

                    <button type="submit" class="pex-btn-primary">
                        <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                        <?php esc_html_e( 'Save & Activate Plugin', 'pexally-email-campaigns' ); ?>
                    </button>
                </form>

                <?php if ( $pexemail_status === 'active' || $pexemail_status === 'expired' ) : ?>
                    <table class="pex-table">
                        <tr>
                            <td>Status:</td>
                            <td style="text-align: right;">
                                <span style="background: <?php echo esc_attr( $pexemail_badge_bg ); ?>; color: <?php echo esc_attr( $pexemail_badge_color ); ?>; padding: 6px 12px; border-radius: 20px; font-weight: 800; font-size: 12px; text-transform: uppercase;">
                                    <?php echo esc_html( $pexemail_badge_text ); ?>
                                </span>
                            </td>
                        </tr>
                        <tr>
                            <td>Licensed Email:</td>
                            <td style="text-align: right; font-weight: 700; color: #0f172a;"><?php echo esc_html( $pexemail_licensed_email ); ?></td>
                        </tr>
                        <tr>
                            <td>Valid Till:</td>
                            <td style="text-align: right; font-weight: 800; color: #0f172a;"><?php echo esc_html( $pexemail_expires_at_str ); ?></td>
                        </tr>
                    </table>

                    <?php if ( $pexemail_show_renew ) : ?>
                        <div style="margin-top: 30px;">
                            <a href="https://payment.pexally.com/pexally-secure-checkout/" target="_blank" class="pex-btn-primary" style="background: #0f172a;">
                                <?php esc_html_e( 'Renew License Now →', 'pexally-email-campaigns' ); ?>
                            </a>
                        </div>
                    <?php endif; ?>
                <?php endif; ?>
            </div>
        </div>

        <div class="pex-card">
            <div class="pex-card-body">
                <h2 class="pex-card-title">
                    <?php esc_html_e( 'Plan Limits & Upgrades', 'pexally-email-campaigns' ); ?>
                    <span style="background: #eff6ff; color: #2563eb; padding: 6px 14px; border-radius: 20px; font-size: 13px; font-weight: 800;">$3.5 / month</span>
                </h2>
                
                <p style="color: #64748b; font-size: 14.5px; margin-bottom: 25px; line-height: 1.7;">
                    <?php esc_html_e( 'Upgrade to Pro to overcome standard server limitations. Available in 6-month and 12-month plans.', 'pexally-email-campaigns' ); ?>
                </p>

                <ul class="pex-list">
                    <li>
                        <span class="pex-list-icon">✓</span> 
                        <div><strong>Free Plan:</strong> <?php esc_html_e( 'Limited to 50 emails per day.', 'pexally-email-campaigns' ); ?></div>
                    </li>
                    <li>
                        <span class="pex-list-icon">✓</span> 
                        <div>
                            <strong>Pro + Standard Gmail:</strong> <?php esc_html_e( 'Send up to', 'pexally-email-campaigns' ); ?> <strong style="color: #10b981;">450 emails/day</strong>. 
                            <br><span style="color: #64748b; font-size: 13px;">(For free @gmail.com accounts)</span>
                        </div>
                    </li>
                    
                    <li class="pex-highlight-block">
                        <span class="pex-list-icon" style="font-size: 20px;">🚀</span> 
                        <div>
                            <strong style="color: #065f46; font-size: 15px;">Pro + Google Workspace:</strong> <?php esc_html_e( 'Send up to', 'pexally-email-campaigns' ); ?> <strong style="color: #059669; font-size: 15px;">1,800 emails/day</strong>. 
                            <br><span style="color: #047857; font-size: 13px; font-weight: 500; display: inline-block; margin-top: 4px;">(Includes our Primary Inbox Placement Guarantee)</span>
                        </div>
                    </li>

                    <li>
                        <span class="pex-list-icon">✓</span> 
                        <div>
                            <strong>Pro + Standard SMTP:</strong> <strong style="color: #10b981;"><?php esc_html_e( 'Unlimited by Plugin', 'pexally-email-campaigns' ); ?></strong>. 
                            <br><span style="color: #64748b; font-size: 13px;">(Subject to your provider's limits. E.g. Amazon SES, SendGrid)</span>
                        </div>
                    </li>
                </ul>

                <div class="pex-warning-bar">
                    <span class="icon">💡</span>
                    <div>
                        <h4><?php esc_html_e( 'Important Rule', 'pexally-email-campaigns' ); ?></h4>
                        <p><?php esc_html_e( '1 license is required per sending email address to successfully overcome standard server sending limits.', 'pexally-email-campaigns' ); ?></p>
                    </div>
                </div>

                <div style="margin-top: 30px;">
                    <a href="https://payment.pexally.com/pexally-secure-checkout/" target="_blank" class="pex-btn-primary" style="background: #0f172a;">
                        <?php esc_html_e( 'Purchase License Key →', 'pexally-email-campaigns' ); ?>
                    </a>
                </div>

                <div class="pex-contact-note">
                    <?php esc_html_e( "Don't have PayPal or a Credit Card?", 'pexally-email-campaigns' ); ?><br>
                    <a href="mailto:admin@pexally.com">Contact us at admin@pexally.com</a>
                </div>
            </div>
        </div>

    </div>
</div>