<?php
/**
 * 247ChatMate — Setup Plan Key Test Reset Partial
 * Checkpoint: TESTRESETCLEANINSTALLLOCK1A
 *
 * Purpose:
 * - Show the test-only clean install reset inside the visible Setup > Plan key box.
 * - Keep the reset off by default and protected by a separate hidden action form.
 *
 * Does not:
 * - Save normal settings.
 * - Change licence validation, widget rendering, chips, WhatsApp handoff, leads, or weekly summary logic.
 */

defined('ABSPATH') || exit;

$matewa_test_reset_available = function_exists('matewa_test_reset_is_available') ? matewa_test_reset_is_available() : false;
$matewa_test_reset_host      = function_exists('matewa_test_reset_get_site_host') ? matewa_test_reset_get_site_host() : '';
$matewa_test_reset_notice    = isset($_GET['matewa_test_reset_notice']) ? sanitize_key(wp_unslash($_GET['matewa_test_reset_notice'])) : '';
$matewa_test_reset_message   = function_exists('matewa_test_reset_notice_message') ? matewa_test_reset_notice_message($matewa_test_reset_notice) : ['', ''];
$matewa_test_reset_tone      = $matewa_test_reset_message[0] ?? '';
$matewa_test_reset_text      = $matewa_test_reset_message[1] ?? '';
$matewa_test_reset_remote    = isset($_GET['matewa_test_reset_remote']) ? sanitize_key(wp_unslash($_GET['matewa_test_reset_remote'])) : '';
$matewa_test_reset_last      = get_option('matewa_test_reset_last_result', []);
$matewa_test_reset_last      = is_array($matewa_test_reset_last) ? $matewa_test_reset_last : [];
$matewa_test_reset_deleted   = is_array($matewa_test_reset_last['deleted'] ?? null) ? $matewa_test_reset_last['deleted'] : [];
?>

<details class="matewa-plan-test-reset-card" data-matewa-test-reset-card="1">
    <summary>
        <span>
            <strong>Test clean install reset</strong>
            <small>Off by default. Controlled test wipe for repeated install testing.</small>
        </span>
        <em>Testing only</em>
    </summary>

    <div class="matewa-plan-test-reset-card__body">
        <?php if ('' !== $matewa_test_reset_text) : ?>
            <div class="matewa-test-reset-notice is-<?php echo esc_attr($matewa_test_reset_tone); ?>" role="status" aria-live="polite">
                <?php echo esc_html($matewa_test_reset_text); ?>
                <?php if ('clean_install_reset' === $matewa_test_reset_notice && '' !== $matewa_test_reset_remote) : ?>
                    <br><small>Remote registry reset: <?php echo esc_html($matewa_test_reset_remote); ?></small>
                <?php endif; ?>
            </div>
        <?php endif; ?>

        <?php if (!$matewa_test_reset_available) : ?>
            <p class="matewa-test-reset-muted">
                This test reset tool is not available on this site. It is limited to 247trademate.com unless explicitly enabled in wp-config.php.
            </p>
        <?php else : ?>
            <p class="matewa-test-reset-muted">
                Use this only before repeated testing. It clears local 247ChatMate settings, licence state, trial state, lead history, chip overrides and MateWA transients for this site. It does not change unrelated plugins or theme settings.
            </p>

            <?php if (!empty($matewa_test_reset_deleted)) : ?>
                <div class="matewa-test-reset-last-run" aria-label="Last test reset result">
                    <strong>Last reset record</strong>
                    <span>Options: <?php echo esc_html((string) absint($matewa_test_reset_deleted['matewa_options'] ?? 0)); ?></span>
                    <span>Transients: <?php echo esc_html((string) (absint($matewa_test_reset_deleted['matewa_transients'] ?? 0) + absint($matewa_test_reset_deleted['matewa_transient_times'] ?? 0))); ?></span>
                </div>
            <?php endif; ?>

            <div class="matewa-test-reset-form" aria-label="Test clean install reset controls">
                <label class="matewa-test-reset-toggle">
                    <input form="matewa-test-reset-form" type="checkbox" name="matewa_test_reset_enabled_confirm" value="1">
                    <span>Turn on clean install reset for this action only</span>
                </label>

                <label>
                    <span>Confirm site domain</span>
                    <input form="matewa-test-reset-form" type="text" name="matewa_test_reset_host_confirm" value="" placeholder="<?php echo esc_attr($matewa_test_reset_host); ?>" autocomplete="off">
                    <small>Type <?php echo esc_html($matewa_test_reset_host); ?> exactly.</small>
                </label>

                <label>
                    <span>Confirm reset phrase</span>
                    <input form="matewa-test-reset-form" type="text" name="matewa_test_reset_phrase" value="" placeholder="RESET 247CHATMATE" autocomplete="off">
                    <small>Type RESET 247CHATMATE exactly.</small>
                </label>

                <label>
                    <span>Temporary reset password or master password</span>
                    <input form="matewa-test-reset-form" type="password" name="matewa_test_reset_password" value="" autocomplete="new-password">
                    <small>The password is checked only for this action and is not printed back into the page.</small>
                </label>

                <button form="matewa-test-reset-form" type="submit" class="button button-secondary matewa-test-reset-button">Run clean install reset</button>
            </div>
        <?php endif; ?>
    </div>
</details>
