<?php
declare(strict_types=1);

if (!isset($_GET['token']) || (string) $_GET['token'] !== 'aysa-reset-2026') {
    http_response_code(403);
    exit('forbidden');
}

$root = dirname(__DIR__, 4);
$wpLoad = $root . '/wp-load.php';
if (!file_exists($wpLoad)) {
    http_response_code(500);
    exit('wp-load-missing');
}

require_once $wpLoad;

if (!function_exists('delete_option')) {
    http_response_code(500);
    exit('wp-not-loaded');
}

delete_option('aysa_active_access_key');
delete_option('aysa_user_secret_key');
delete_option('aysa_user_id');
delete_option('aysa_auth_domain');

global $wpdb;
$domain = (string) wp_parse_url(home_url(), PHP_URL_HOST);
$domain = strtolower(preg_replace('/^www\./', '', trim($domain)));
if ($domain !== '') {
    $table_name = $wpdb->prefix . 'aysa_user_auth';
    $wpdb->delete($table_name, array('domain' => $domain), array('%s'));
}

header('Content-Type: application/json; charset=utf-8');
echo wp_json_encode(array(
    'ok' => true,
    'host' => $domain,
    'message' => 'Activation auth reset complete.',
));
