<?php
/**
 * 247ChatMate — Plan Access Overview Partial
 *
 * Phase 5-A-21P
 * Checkpoint: PLANLICENCECUSTOMERVIEW5A21P
 *
 * Purpose:
 * - Keep the public admin Plan & Licence view customer-safe and calm.
 * - Show only the plan, countdown/action and practical inclusions.
 *
 * Does not:
 * - Validate licences, start trials, call registries, save settings or change entitlement decisions.
 * - Change widget, chip, WhatsApp handoff, phone selector or lead routing behaviour.
 */

defined('ABSPATH') || exit;

$matewa_is_founder_beta_plan = ('founder_beta_registry' === ($license_source ?? '')) || !empty($founder_beta_is_registered);
$matewa_pro_like_access      = in_array($mode, ['trial_active', 'pro', 'master'], true) || $matewa_is_founder_beta_plan;

$matewa_direct_trial_build = function_exists('matewa_distribution_has_pro_trial') && matewa_distribution_has_pro_trial();
$matewa_checkout_url = defined('MATEWA_SINGLE_MONTHLY_CHECKOUT_URL')
    ? MATEWA_SINGLE_MONTHLY_CHECKOUT_URL
    : 'https://247chatmate.lemonsqueezy.com/checkout/buy/654461de-2eae-4715-a249-a2ac7e032678';

$matewa_plan_title       = 'Free plan';
$matewa_plan_message     = $matewa_direct_trial_build
    ? 'Start your 28-day Pro trial when you are ready.'
    : 'Free access is active. Go Pro when you are ready.';
$matewa_next_step        = $matewa_direct_trial_build ? 'Start your 28-day Pro trial.' : 'Upgrade to Pro.';
$matewa_plan_tone        = 'is-free';
$matewa_countdown_label  = $matewa_direct_trial_build ? 'Trial not started' : 'Free';
$matewa_countdown_detail = $matewa_direct_trial_build ? 'Your 28-day countdown starts when you press Start trial.' : 'No trial on this WordPress.org build.';
$matewa_primary_action   = $matewa_direct_trial_build ? 'Start 28-day Pro trial' : '';
$matewa_primary_href     = '';
$matewa_primary_kind     = $matewa_direct_trial_build ? 'start_trial' : 'none';

if ($matewa_is_founder_beta_plan) {
    $matewa_plan_title       = 'Founder Beta is active';
    $matewa_plan_message     = 'Founder Beta is giving this site Pro access while the beta window remains active.';
    $matewa_next_step        = $founder_beta_is_registered ? 'Keep building and convert before expiry.' : 'No action needed right now.';
    $matewa_plan_tone        = 'is-pro';
    $matewa_countdown_label  = $founder_beta_is_registered ? $founder_beta_days_label : 'Active';
    $matewa_countdown_detail = $founder_beta_is_registered ? 'Founder Beta expiry: ' . $founder_beta_expiry_display : 'Founder Beta status is active.';
    $matewa_primary_action   = '';
    $matewa_primary_href     = '#matewa-plan-actions-card';
    $matewa_primary_kind     = 'none';
} elseif ('pro' === $mode) {
    $matewa_plan_title       = 'Pro is active';
    $matewa_plan_message     = 'Your saved licence has moved this site into Pro. Your lead tools and widget settings remain in place.';
    $matewa_next_step        = 'No action needed.';
    $matewa_plan_tone        = 'is-pro';
    $matewa_countdown_label  = 'Active';
    $matewa_countdown_detail = 'No trial countdown needed while Pro is active.';
    $matewa_primary_action   = '';
    $matewa_primary_href     = '#matewa-plan-actions-card';
    $matewa_primary_kind     = 'none';
} elseif ('master' === $mode) {
    $matewa_plan_title       = 'Master access is active';
    $matewa_plan_message     = 'Master access is enabled on this site.';
    $matewa_next_step        = 'No licence check needed.';
    $matewa_plan_tone        = 'is-master';
    $matewa_countdown_label  = 'Active';
    $matewa_countdown_detail = 'Master access does not use a customer countdown.';
    $matewa_primary_action   = 'Open setup';
    $matewa_primary_href     = '#matewa-core-settings-section';
    $matewa_primary_kind     = 'link';
} elseif ('trial_active' === $mode) {
    $matewa_plan_title       = 'Trial is active';
    $matewa_plan_message     = $trial_remaining_label . '. Add a licence before the trial ends to keep Pro active.';
    $matewa_next_step        = 'Add a licence before expiry.';
    $matewa_plan_tone        = 'is-trial';
    $matewa_countdown_label  = $trial_remaining_label;
    $matewa_countdown_detail = 'Trial ends: ' . $trial_end_display;
    $matewa_primary_action   = '';
    $matewa_primary_href     = '';
    $matewa_primary_kind     = 'none';
} elseif ('trial_expired' === $mode) {
    $matewa_plan_title       = 'Trial has expired';
    $matewa_plan_message     = 'Your saved setup is preserved. Add a valid licence to reactivate Pro.';
    $matewa_next_step        = 'Add a valid licence key.';
    $matewa_plan_tone        = 'is-expired';
    $matewa_countdown_label  = 'Expired';
    $matewa_countdown_detail = 'Your saved settings are still preserved.';
    $matewa_primary_action   = 'Go Pro';
    $matewa_primary_href     = $matewa_checkout_url;
    $matewa_primary_kind     = 'checkout';
}

$matewa_feature_status = $matewa_pro_like_access ? 'Included' : 'Pro';
$matewa_key_status     = $has_key ? 'Key saved' : 'No key saved';
?>

<div class="matewa-plan-overview matewa-plan-overview--customer <?php echo esc_attr($matewa_plan_tone); ?>" aria-label="Plan and licence overview">
    <div class="matewa-plan-overview__copy">
        <span class="matewa-status-badge <?php echo esc_attr($status_badge_class); ?>">
            <?php echo esc_html($status_badge_text); ?>
        </span>
        <h3><?php echo esc_html($matewa_plan_title); ?></h3>
        <p><?php echo esc_html($matewa_plan_message); ?></p>
    </div>
</div>

<?php if ($matewa_direct_trial_build) : ?>
    <div class="matewa-direct-trial-status" aria-label="Direct Trial status">
        <?php if ('trial_active' === $mode) : ?>
            <div>
                <span>State</span>
                <strong>Trial active</strong>
            </div>
            <div>
                <span>Started</span>
                <strong><?php echo esc_html($trial_started_display); ?></strong>
            </div>
            <div>
                <span>Time remaining</span>
                <strong><?php echo esc_html($trial_remaining_label); ?></strong>
            </div>
            <div>
                <span>Expires</span>
                <strong><?php echo esc_html($trial_end_display); ?></strong>
            </div>
        <?php elseif ('trial_expired' === $mode) : ?>
            <div>
                <span>State</span>
                <strong>Free — trial expired</strong>
            </div>
            <div>
                <span>Trial started</span>
                <strong><?php echo esc_html($trial_started_display); ?></strong>
            </div>
            <div>
                <span>Trial ended</span>
                <strong><?php echo esc_html($trial_end_display); ?></strong>
            </div>
        <?php elseif ('free' === $mode) : ?>
            <div>
                <span>State</span>
                <strong>Free</strong>
            </div>
            <div>
                <span>Trial</span>
                <strong>Not started</strong>
            </div>
            <div>
                <span>Trial length</span>
                <strong><?php echo esc_html((string) $trial_days); ?> days</strong>
            </div>
        <?php endif; ?>
    </div>
<?php endif; ?>

<div class="matewa-plan-feature-grid" aria-label="What your plan includes">
    <div class="matewa-plan-feature-card">
        <span class="dashicons dashicons-format-chat" aria-hidden="true"></span>
        <strong>Website widget</strong>
        <small>Visitors can start the WhatsApp enquiry flow.</small>
    </div>

    <div class="matewa-plan-feature-card">
        <span class="dashicons dashicons-email-alt" aria-hidden="true"></span>
        <strong>Lead emails</strong>
        <small><?php echo esc_html($matewa_feature_status); ?> for lead capture and follow-up.</small>
    </div>

    <div class="matewa-plan-feature-card">
        <span class="dashicons dashicons-chart-line" aria-hidden="true"></span>
        <strong>Weekly summary</strong>
        <small><?php echo esc_html($matewa_feature_status); ?> to show value without logging in.</small>
    </div>

    <div class="matewa-plan-feature-card">
        <span class="dashicons dashicons-admin-network" aria-hidden="true"></span>
        <strong>Settings preserved</strong>
        <small>Your setup stays in place across plan changes.</small>
    </div>
</div>

<div class="matewa-plan-next-step" aria-label="Recommended next step">
    <div>
        <span>Next step</span>
        <strong><?php echo esc_html($matewa_next_step); ?></strong>
        <small><?php echo esc_html($matewa_key_status); ?></small>
    </div>
    <?php if ('start_trial' === ($matewa_primary_kind ?? '') && '' !== $matewa_primary_action) : ?>
        <?php if (!empty($marketing_email) && is_email($marketing_email) && $trial_whatsapp_saved) : ?>
            <form method="post" action="" class="matewa-plan-primary-action">
                <?php wp_nonce_field('matewa_start_trial_nonce', 'matewa_start_trial_nonce'); ?>
                <input type="hidden" name="matewa_start_trial_action" value="1" />
                <button type="submit" class="button button-primary"><?php echo esc_html($matewa_primary_action); ?></button>
            </form>
        <?php elseif (empty($marketing_email) || !is_email($marketing_email)) : ?>
            <a class="button button-primary" href="#matewa_marketing_email">Add account email to start trial</a>
        <?php else : ?>
            <a class="button button-primary" href="#matewa_number">Add WhatsApp number to start trial</a>
        <?php endif; ?>
    <?php elseif ('' !== $matewa_primary_action) : ?>
        <a class="button button-primary" href="<?php echo esc_url($matewa_primary_href); ?>"<?php echo 'checkout' === ($matewa_primary_kind ?? '') ? ' target="_blank" rel="noopener noreferrer"' : ''; ?>>
            <?php echo esc_html($matewa_primary_action); ?>
        </a>
    <?php endif; ?>
</div>
