<?php
/**
 * GT BOGO Engine — License Settings
 * @package GT_BOGO_Engine
 * @since   3.10.1
 */
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! current_user_can( 'manage_options' ) ) return;

$license_manager = function_exists( 'gt_bogo_license_manager' ) ? gt_bogo_license_manager() : null;
$license_key     = $license_manager ? $license_manager->get_license_key() : '';
$license_status  = $license_manager ? $license_manager->get_status_details() : [];
$message         = '';
$message_type    = '';

if ( isset( $_POST['gtbogo_save_license'] ) && check_admin_referer( 'gtbogo_license', 'gtbogo_license_nonce' ) ) {
    if ( $license_manager ) {
        $new_key = sanitize_text_field( wp_unslash( $_POST['gtbogo_license_key'] ?? '' ) );
        $license_manager->save_license_key( $new_key );
        $license_manager->refresh();
        $license_key    = $new_key;
        $license_status = $license_manager->get_status_details();
        $message        = 'License key saved and verified.';
        $message_type   = 'success';
    }
}

require_once __DIR__ . '/../ui-system/wrapper-start.php';
?>
<div class="gt-page-header gt-animate-in">
    <div>
        <h1 class="gt-page-title">🔑 License Activation</h1>
        <p class="gt-page-subtitle">Enter your GT BOGO Engine PRO license key to unlock all features</p>
    </div>
</div>

<?php if ( $message ) : ?>
<div class="gt-notice gt-notice-<?php echo esc_attr( $message_type ); ?> gt-animate-in"><?php echo esc_html( $message ); ?></div>
<?php endif; ?>

<div class="gt-card gt-animate-in" style="max-width:680px">
    <div class="gt-card-header">
        <h2 class="gt-card-title">License Key</h2>
    </div>

    <form method="post" action="">
        <?php wp_nonce_field( 'gtbogo_license', 'gtbogo_license_nonce' ); ?>
        <div class="gt-form-group">
            <label class="gt-form-label">License Key</label>
            <input type="text" name="gtbogo_license_key"
                   class="gt-input" style="font-family:'DM Mono',monospace;letter-spacing:1px"
                   value="<?php echo esc_attr( $license_key ); ?>"
                   placeholder="GTBOGO-XXXX-XXXX-XXXX" />
            <p class="gt-text-soft gt-text-sm" style="margin-top:8px">
                Enter your GT BOGO Engine PRO license key.
                <a href="https://graphictshirts.shop/bogo/" target="_blank" rel="noopener" style="color:var(--gt-red)">Purchase a license</a>
            </p>
        </div>

        <?php if ( ! empty( $license_key ) && ! empty( $license_status ) ) :
            $s        = $license_status['status'] ?? 'unknown';
            $pill_map = [
                'active'   => ['bg'=>'rgba(34,197,94,0.08)',  'border'=>'rgba(34,197,94,0.25)',  'color'=>'#86efac', 'label'=>'✓ Active'],
                'expiring' => ['bg'=>'rgba(245,158,11,0.08)', 'border'=>'rgba(245,158,11,0.25)', 'color'=>'#fcd34d', 'label'=>'⚠ Expiring Soon'],
                'expired'  => ['bg'=>'rgba(239,68,68,0.08)',  'border'=>'rgba(239,68,68,0.25)',  'color'=>'#f87171', 'label'=>'✗ Expired'],
                'invalid'  => ['bg'=>'rgba(239,68,68,0.08)',  'border'=>'rgba(239,68,68,0.25)',  'color'=>'#f87171', 'label'=>'✗ Invalid'],
            ];
            $pill = $pill_map[ $s ] ?? ['bg'=>'rgba(100,116,139,0.08)','border'=>'rgba(100,116,139,0.25)','color'=>'#94a3b8','label'=>'Unknown'];
        ?>
        <div class="gt-card" style="background:var(--gt-card-alt);margin-bottom:20px">
            <table width="100%" cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td style="padding:10px 0;border-bottom:1px solid var(--gt-border);font-size:13px;font-weight:600;color:var(--gt-soft);width:40%">Status</td>
                    <td style="padding:10px 0;border-bottom:1px solid var(--gt-border);text-align:right">
                        <span style="background:<?php echo $pill['bg']; ?>;border:1px solid <?php echo $pill['border']; ?>;color:<?php echo $pill['color']; ?>;padding:3px 12px;border-radius:50px;font-size:12px;font-weight:700">
                            <?php echo $pill['label']; ?>
                        </span>
                    </td>
                </tr>
                <?php if ( ! empty( $license_status['expires_human'] ) ) : ?>
                <tr>
                    <td style="padding:10px 0;border-bottom:1px solid var(--gt-border);font-size:13px;font-weight:600;color:var(--gt-soft)">Expires</td>
                    <td style="padding:10px 0;border-bottom:1px solid var(--gt-border);text-align:right;font-size:13px;font-weight:700;color:var(--gt-white)"><?php echo esc_html( $license_status['expires_human'] ); ?></td>
                </tr>
                <?php endif; ?>
                <?php if ( isset( $license_status['days_remaining'] ) ) : ?>
                <tr>
                    <td style="padding:10px 0;border-bottom:1px solid var(--gt-border);font-size:13px;font-weight:600;color:var(--gt-soft)">Days Remaining</td>
                    <td style="padding:10px 0;border-bottom:1px solid var(--gt-border);text-align:right;font-size:13px;font-weight:700;color:var(--gt-white)"><?php echo intval( $license_status['days_remaining'] ); ?></td>
                </tr>
                <?php endif; ?>
                <tr>
                    <td style="padding:10px 0;font-size:13px;font-weight:600;color:var(--gt-soft)">Valid For</td>
                    <td style="padding:10px 0;text-align:right;font-size:13px;font-weight:700;color:var(--gt-white)">1 website</td>
                </tr>
            </table>
        </div>

        <?php if ( in_array( $s, ['expired','expiring'], true ) && $license_manager && method_exists( $license_manager, 'get_renewal_url' ) ) : ?>
        <div style="margin-bottom:20px">
            <a href="<?php echo esc_url( $license_manager->get_renewal_url() ); ?>"
               class="gt-btn gt-btn-primary" target="_blank" rel="noopener">Renew License</a>
        </div>
        <?php endif; ?>
        <?php endif; ?>

        <button type="submit" name="gtbogo_save_license" class="gt-btn gt-btn-primary">Save &amp; Verify License</button>
    </form>
</div>

<?php if ( function_exists( 'gt_bogo_has_pro' ) && gt_bogo_has_pro() ) : ?>
<div class="gt-card gt-animate-in" style="max-width:680px;margin-top:0;background:rgba(34,197,94,0.04);border-color:rgba(34,197,94,0.2)">
    <div style="display:flex;align-items:center;gap:14px">
        <span style="font-size:28px">✓</span>
        <div>
            <div style="font-size:14px;font-weight:700;color:#86efac;margin-bottom:3px">PRO Features Unlocked</div>
            <div class="gt-text-soft gt-text-sm">All enterprise features are active on this installation.</div>
        </div>
    </div>
</div>
<?php endif; ?>
<?php require_once __DIR__ . '/../ui-system/wrapper-end.php'; ?>
