<?php /* developer/clear_confirm.php */
$scope = $_GET['scope'] ?? 'transactions';
$scopeConfig = [
  'transactions' => [
    'label'    => 'Transaction Data',
    'color'    => 'danger',
    'icon'     => 'bi-receipt-cutoff',
    'warning'  => 'This will permanently delete all sales, purchases, expenses, cheques, stock movements and payment records.',
    'keeps'    => 'Users, Roles, Settings, Products, Customers (balances reset to 0), Suppliers (balances reset to 0), Branches.',
    'deletes'  => 'Sales Invoices, Sale Items, Sale Payments, Purchases, Purchase Items, Expenses, Cheque Records, Customer Payments, Stock Movements.',
  ],
  'full' => [
    'label'    => 'FULL User Data',
    'color'    => 'danger',
    'icon'     => 'bi-nuclear',
    'warning'  => '⚠ DANGER: This clears EVERYTHING including customers, suppliers and products. This cannot be undone.',
    'keeps'    => 'Users, Roles, Permissions, Settings, Branches, Categories, Brands, Units.',
    'deletes'  => 'ALL transactions, Returns, Held Bills + Customers (except Walk-in) + Suppliers + Products + Stock + Activity Logs.',
  ],
  'full_reset' => [
    'label'    => 'Full Reset (Wipe & Reset IDs)',
    'color'    => 'danger',
    'icon'     => 'bi-arrow-counterclockwise',
    'warning'  => '⚠ This deletes all transactions, products and branches, resets Customer/Supplier balances to 0 (records kept), and resets ALL table auto-increment IDs back to 1. This cannot be undone.',
    'keeps'    => 'Users, Roles, Permissions, Settings, Categories, Brands, Units, Customer & Supplier records (balances reset to 0).',
    'deletes'  => 'All Transactions (Sales, Purchases, Expenses, Cheques, Stock Movements, Activity Logs), Products, Branches. Customers & Suppliers: balances reset to 0 (NOT deleted). All table IDs reset to 1.',
  ],
  'logs' => [
    'label'    => 'Activity Logs',
    'color'    => 'warning',
    'icon'     => 'bi-journal-x',
    'warning'  => 'All activity log entries will be permanently deleted.',
    'keeps'    => 'Everything except activity logs.',
    'deletes'  => 'Activity Logs only.',
  ],
];
$cfg = $scopeConfig[$scope] ?? $scopeConfig['transactions'];
?>

<div class="d-flex align-items-center gap-3 mb-3">
  <a href="<?= APP_URL ?>/developer"
     class="btn btn-sm btn-outline-secondary">
    <i class="bi bi-arrow-left me-1"></i>Back
  </a>
  <div>
    <h5 class="fw-bold mb-0 text-danger">
      <i class="bi <?= $cfg['icon'] ?> me-2"></i>
      Clear: <?= $cfg['label'] ?>
    </h5>
    <small class="text-muted">This action cannot be undone</small>
  </div>
</div>

<?= flashMessages() ?>

<!-- Warning card -->
<div class="row justify-content-center">
  <div class="col-lg-7">

    <!-- Danger alert -->
    <div class="alert alert-danger border-danger mb-4 d-flex gap-3"
         style="background:#fef2f2;border-left:5px solid #dc2626!important">
      <i class="bi bi-exclamation-octagon-fill text-danger fs-3 flex-shrink-0 mt-1"></i>
      <div>
        <div class="fw-bold mb-1">WARNING — Destructive Operation</div>
        <div class="small"><?= $cfg['warning'] ?></div>
      </div>
    </div>

    <!-- What gets deleted/kept -->
    <div class="row g-3 mb-4">
      <div class="col-6">
        <div class="rounded-3 p-3 h-100"
             style="background:#fef2f2;border:1px solid #fecaca">
          <div class="fw-bold text-danger small mb-2">
            <i class="bi bi-trash3 me-1"></i>WILL BE DELETED
          </div>
          <div class="small text-danger"><?= $cfg['deletes'] ?></div>
        </div>
      </div>
      <div class="col-6">
        <div class="rounded-3 p-3 h-100"
             style="background:#f0fdf4;border:1px solid #bbf7d0">
          <div class="fw-bold text-success small mb-2">
            <i class="bi bi-shield-check me-1"></i>WILL BE KEPT
          </div>
          <div class="small text-success"><?= $cfg['keeps'] ?></div>
        </div>
      </div>
    </div>

    <!-- Double confirmation form -->
    <div class="card border-danger">
      <div class="card-header bg-danger text-white fw-bold">
        <i class="bi bi-lock me-1"></i>Confirm to Proceed
      </div>
      <div class="card-body">
        <form method="POST" action="<?= APP_URL ?>/developer/clear-execute"
              onsubmit="return validateForm()">
          <?= Auth::csrfField() ?>
          <input type="hidden" name="scope" value="<?= e($scope) ?>">

          <!-- Step 1: Type confirmation -->
          <div class="mb-3">
            <label class="form-label fw-semibold">
              Step 1: Type <kbd class="bg-danger text-white px-2">CLEAR ALL DATA</kbd>
              exactly to confirm
            </label>
            <input type="text" name="confirm_text" id="confirmText"
                   class="form-control border-danger fw-semibold"
                   placeholder="Type: CLEAR ALL DATA"
                   autocomplete="off" spellcheck="false">
          </div>

          <!-- Step 2: Checkbox -->
          <div class="mb-4 p-3 rounded" style="background:#fef2f2">
            <div class="form-check">
              <input type="checkbox" name="confirm_check" value="1"
                     class="form-check-input border-danger" id="confirmCheck">
              <label class="form-check-label fw-semibold text-danger" for="confirmCheck">
                I understand this action is <strong>irreversible</strong> and will
                permanently delete <?= $cfg['label'] ?> data.
              </label>
            </div>
          </div>

          <!-- Submit -->
          <div class="d-flex gap-2 justify-content-between">
            <a href="<?= APP_URL ?>/developer"
               class="btn btn-outline-secondary fw-semibold px-4">
              <i class="bi bi-x-circle me-1"></i>Cancel
            </a>
            <button type="submit" id="executeBtn"
                    class="btn btn-danger fw-bold px-5" disabled>
              <i class="bi bi-trash3-fill me-1"></i>
              EXECUTE CLEAR — <?= strtoupper($cfg['label']) ?>
            </button>
          </div>
        </form>
      </div>
    </div>

    <!-- Logged warning -->
    <div class="text-center mt-3 text-muted small">
      <i class="bi bi-camera me-1"></i>
      This action will be recorded in the activity log with your username,
      IP address and timestamp.
    </div>
  </div>
</div>

<script>
const confirmText = document.getElementById('confirmText');
const confirmCheck = document.getElementById('confirmCheck');
const btn = document.getElementById('executeBtn');

function checkReady() {
  const textOk  = confirmText.value === 'CLEAR ALL DATA';
  const checkOk = confirmCheck.checked;
  btn.disabled  = !(textOk && checkOk);
  confirmText.style.borderColor = confirmText.value.length > 0
    ? (textOk ? '#16a34a' : '#dc2626') : '';
}

confirmText.addEventListener('input', checkReady);
confirmCheck.addEventListener('change', checkReady);

function validateForm() {
  if (confirmText.value !== 'CLEAR ALL DATA' || !confirmCheck.checked) {
    alert('Please complete all confirmation steps.');
    return false;
  }
  return confirm('⚠ FINAL WARNING: This will permanently delete data. Continue?');
}
</script>