<?php
require_once __DIR__.'/../includes/functions.php';
require_once __DIR__.'/../includes/v292_admin_portal_helpers.php';
$admin=require_super_admin(); mj292_ensure_schema(); verify_csrf();
$adminTitle='Safe Reset System'; $adminSubtitle='Start fresh by clearing selected operational data. Super admin password and RESET confirmation required.';
$map=mj292_reset_map();
if ($_SERVER['REQUEST_METHOD']==='POST') {
    $confirm=trim((string)($_POST['confirm_text']??''));
    $password=(string)($_POST['admin_password']??'');
    $options=(array)($_POST['reset_options']??[]);
    if ($confirm !== 'RESET') { flash('error','Please type RESET exactly to confirm.'); redirect('system-reset.php'); }
    if (!$password || !password_verify($password, (string)($admin['password_hash'] ?? ''))) { flash('error','Admin password is not correct. Reset blocked.'); redirect('system-reset.php'); }
    if (!$options) { flash('error','Please choose at least one reset option.'); redirect('system-reset.php'); }
    $result=mj292_run_reset($options,$admin);
    flash('success','Safe reset completed. Records deleted: '.$result['records'].'. Files deleted: '.$result['files'].'.');
    redirect('report-reset-logs.php');
}
include __DIR__.'/_header.php';
?>
<div class="alert alert-danger"><h5><i class="fas fa-exclamation-triangle mr-1"></i>Permanent action</h5>This reset can delete selected users, listings, companies, payments, wallet records, receipts, notifications and related uploaded files. Take database and upload backup before running.</div>
<div class="row"><div class="col-lg-8"><div class="card mj-v292-danger-zone"><div class="card-body">
<form method="post" onsubmit="return confirm('Final warning: this reset is permanent. Continue?');"><?=csrf_input()?>
  <h4 class="font-weight-bold mb-3">Choose reset options</h4>
  <div class="row">
    <div class="col-md-6 mb-3"><label class="border rounded p-3 d-block h-100"><input type="checkbox" name="reset_options[]" value="all"> <b>All Operational Data</b><br><small class="text-muted">Users, listings, companies, payments, wallets, receipts and notifications.</small></label></div>
    <?php foreach($map as $key=>$m): ?><div class="col-md-6 mb-3"><label class="border rounded p-3 d-block h-100"><input type="checkbox" name="reset_options[]" value="<?=e($key)?>"> <b><?=e($m['label'])?></b><br><small class="text-muted">Tables: <?=e(implode(', ', array_slice($m['tables'],0,5)))?><?=count($m['tables'])>5?'...':''?></small></label></div><?php endforeach; ?>
  </div>
  <hr><div class="row"><div class="col-md-6"><label>Type RESET</label><input class="form-control" name="confirm_text" required placeholder="RESET"></div><div class="col-md-6"><label>Current super admin password</label><input class="form-control" type="password" name="admin_password" required></div></div>
  <button class="btn btn-danger btn-lg mt-4"><i class="fas fa-trash mr-1"></i>Run Safe Reset</button>
</form>
</div></div></div><div class="col-lg-4"><div class="card mj-v292-card"><div class="card-body"><h5 class="font-weight-bold">Protected</h5><ul class="mb-0"><li>Super admin account</li><li>System settings</li><li>Countries and categories unless selected elsewhere</li><li>PHP/config/source files</li><li>Payment method setup</li></ul><hr><a href="backup.php" class="btn btn-outline-primary btn-block">Backup / Health</a><a href="report-reset-logs.php" class="btn btn-outline-secondary btn-block">View Reset Logs</a></div></div></div></div>
<?php include __DIR__.'/_footer.php'; ?>
