<?php
/**
 * 247ChatMate — Plan Access Actions Partial
 *
 * Phase 5-A-21P
 * Checkpoint: PLANLICENCECUSTOMERVIEW5A21P
 *
 * Purpose:
 * - Keep the customer-facing plan action area simple.
 * - Preserve existing trial and licence check handlers without exposing raw diagnostics.
 *
 * Does not:
 * - Validate licences directly, change entitlement decisions, alter trial rules or save settings.
 * - Change LemonSqueezy, Founder Beta, registry, widget, chip or lead-routing logic.
 */

defined('ABSPATH') || exit;
?>

<div
    id="matewa-plan-actions-card"
    class="matewa-plan-actions-card"
    data-matewa-license-check-card="1"
>
    <div class="matewa-plan-actions-card__copy">
        <h3>Licence Key &amp; refresh plan</h3>
        <p>Edit your key or refresh your plan.</p>
    </div>

    <div class="matewa-plan-actions-card__body">
        <?php if ('free' === $mode) : ?>
            <div class="matewa-plan-action-row matewa-plan-action-row--full">
                <div>
                    <strong>Start trial</strong>
                    <span>Unlock Pro features on this site for <?php echo esc_html((string) $trial_days); ?> days.</span>
                </div>

                <?php if (!empty($marketing_email) && is_email($marketing_email) && $trial_whatsapp_saved) : ?>
                    <form method="post" 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">Start trial</button>
                    </form>
                <?php elseif (empty($marketing_email) || !is_email($marketing_email)) : ?>
                    <a class="button button-secondary" href="#matewa_marketing_email">Add account email</a>
                <?php else : ?>
                    <a class="button button-secondary" href="#matewa_number">Add WhatsApp number</a>
                <?php endif; ?>
            </div>
        <?php endif; ?>

        <div class="matewa-plan-action-row matewa-plan-action-row--tile">
            <div>
                <strong>Licence Key</strong>
                <span><?php echo $has_key ? 'Key saved.' : 'Add key in Setup.'; ?></span>
            </div>
            <a class="button button-secondary" href="#matewa_license_key"><?php echo $has_key ? 'View key field' : 'Add key'; ?></a>
        </div>

        <div class="matewa-plan-action-row matewa-plan-action-row--tile">
            <div>
                <strong>Refresh plan</strong>
                <span>Recheck saved access.</span>
            </div>

            <?php if (!$has_key) : ?>
                <button type="button" class="button button-secondary" disabled>Refresh plan</button>
            <?php elseif ($is_master) : ?>
                <button type="button" class="button button-secondary" disabled>Refresh plan</button>
            <?php elseif ($is_dev_forced) : ?>
                <button type="button" class="button button-secondary" disabled>Refresh plan</button>
            <?php else : ?>
                <form method="post" action="<?php echo esc_url(admin_url('admin.php?page=247chatmate')); ?>" data-matewa-license-check-form="1">
                    <?php wp_nonce_field('matewa_check_license_nonce', 'matewa_check_license_nonce'); ?>
                    <input type="hidden" name="matewa_check_license_action" value="1" />
                    <button type="submit" class="button button-secondary">Refresh plan</button>
                </form>
            <?php endif; ?>
        </div>
    </div>
</div>
