<?php /* developer/index.php */
$transactionTables = ['sales','sale_items','sale_payments','purchases','purchase_items',
                       'expenses','cheque_tracking','customer_payments','stock_movements'];
$fullCount = array_sum(array_filter(array_map(fn($t)=>$counts[$t]['count']??0, $transactionTables),'is_numeric'));
?>

<style>
.dev-hero{background:linear-gradient(135deg,#0f172a,#1e293b);border-radius:12px;
  padding:20px 24px;color:#fff;margin-bottom:20px}
.dev-badge{background:#ff0000;color:#fff;font-size:.65rem;padding:2px 8px;
  border-radius:4px;font-weight:700;letter-spacing:.05em}
.table-card{border-radius:10px;border:1px solid #e2e8f0;overflow:hidden;margin-bottom:12px}
.table-card-header{background:#1e293b;color:#fff;padding:10px 16px;
  font-size:.8rem;font-weight:700;display:flex;justify-content:space-between;align-items:center}
.count-cell{font-size:1.1rem;font-weight:800;color:#1d4ed8}
.count-zero{color:#9ca3af}
</style>

<div class="dev-hero">
  <div class="d-flex justify-content-between align-items-start flex-wrap gap-2">
    <div>
      <h5 class="fw-bold mb-1 d-flex align-items-center gap-2" style="font-size:1.2rem">
        🛠 Developer Tools
        <span class="dev-badge">RESTRICTED</span>
      </h5>
      <div style="opacity:.7;font-size:.83rem">
        Database: <strong><?= DB_NAME ?? 'mjpos_db' ?></strong>
        &nbsp;·&nbsp; Size: <strong><?= $dbSize['mb'] ?? '?' ?> MB</strong>
        &nbsp;·&nbsp; User: <strong><?= Auth::name() ?></strong>
        &nbsp;·&nbsp; <?= date('d M Y H:i:s') ?>
      </div>
    </div>
    <div class="d-flex gap-2">
      <a href="<?= APP_URL ?>/developer/clear"
         class="btn btn-danger fw-bold px-4">
        <i class="bi bi-trash3-fill me-1"></i>Clear Database
      </a>
    </div>
  </div>
</div>

<?= flashMessages() ?>

<!-- Warning banner -->
<div class="alert border-0 mb-3 d-flex gap-3 align-items-start"
     style="background:#fef2f2;border-left:4px solid #dc2626!important;border-radius:8px">
  <i class="bi bi-shield-exclamation text-danger fs-4 flex-shrink-0 mt-1"></i>
  <div>
    <div class="fw-bold text-danger mb-1">Developer Zone — Restricted Access</div>
    <div class="small text-danger">
      This area contains destructive tools. All actions are logged.
      Only Developer and Super Admin roles have access.
    </div>
  </div>
</div>

<div class="row g-3">

  <!-- Transaction data counts -->
  <div class="col-lg-6">
    <div class="table-card">
      <div class="table-card-header">
        <span><i class="bi bi-database me-2"></i>Transaction Data</span>
        <span class="badge bg-primary"><?= number_format($fullCount) ?> total rows</span>
      </div>
      <table class="table table-sm mb-0">
        <tbody>
          <?php foreach([
            ['sales',           'bi-receipt text-success',  'Sales Invoices'],
            ['sale_items',      'bi-list-ul text-success',  'Sale Items'],
            ['sale_payments',   'bi-cash text-success',     'Sale Payments'],
            ['purchases',       'bi-truck text-info',       'Purchases'],
            ['purchase_items',  'bi-list-ul text-info',     'Purchase Items'],
            ['expenses',        'bi-wallet2 text-danger',   'Expenses'],
            ['cheque_tracking', 'bi-bank text-warning',     'Cheque Records'],
            ['customer_payments','bi-person text-primary',  'Customer Payments'],
            ['stock_movements', 'bi-boxes text-warning',    'Stock Movements'],
          ] as[$tbl,$icon,$lbl]):
            $cnt = $counts[$tbl]['count'] ?? '—';
            $isZero = $cnt === 0 || $cnt === '0';
          ?>
          <tr>
            <td class="ps-3">
              <i class="bi <?=$icon?> me-2"></i>
              <span class="small fw-semibold"><?=$lbl?></span>
            </td>
            <td class="text-end pe-3">
              <span class="<?=$isZero?'count-zero':'count-cell'?>">
                <?= is_numeric($cnt)?number_format((int)$cnt):$cnt ?>
              </span>
            </td>
          </tr>
          <?php endforeach;?>
        </tbody>
        <tfoot style="background:#f8fafc">
          <tr>
            <td class="ps-3 fw-bold small py-2">TOTAL ROWS</td>
            <td class="text-end pe-3 fw-bold text-primary"><?=number_format($fullCount)?></td>
          </tr>
        </tfoot>
      </table>
    </div>
  </div>

  <!-- Other tables + actions -->
  <div class="col-lg-6">
    <div class="table-card mb-3">
      <div class="table-card-header">
        <span><i class="bi bi-shield-check me-2"></i>Protected Data (not cleared)</span>
      </div>
      <table class="table table-sm mb-0">
        <tbody>
          <?php foreach([
            ['products',  'bi-box-seam text-warning',  'Products'],
            ['customers', 'bi-people text-primary',    'Customers'],
            ['suppliers', 'bi-shop text-secondary',    'Suppliers'],
          ] as[$tbl,$icon,$lbl]):
            $cnt = $counts[$tbl]['count'] ?? '—';
          ?>
          <tr>
            <td class="ps-3">
              <i class="bi <?=$icon?> me-2"></i>
              <span class="small fw-semibold"><?=$lbl?></span>
            </td>
            <td class="text-end pe-3">
              <span class="count-cell"><?=is_numeric($cnt)?number_format((int)$cnt):$cnt?></span>
            </td>
          </tr>
          <?php endforeach;?>
        </tbody>
      </table>
    </div>

    <!-- Clear options -->
    <div class="table-card">
      <div class="table-card-header">
        <span><i class="bi bi-trash3 me-2"></i>Clear Options</span>
      </div>
      <div class="p-3 d-flex flex-column gap-2">

        <a href="<?= APP_URL ?>/developer/clear?scope=transactions"
           class="d-flex align-items-start gap-3 p-3 rounded text-decoration-none"
           style="background:#fef2f2;border:1px solid #fecaca">
          <i class="bi bi-receipt-cutoff text-danger fs-4 flex-shrink-0 mt-1"></i>
          <div>
            <div class="fw-bold text-danger small">Clear Transaction Data</div>
            <div class="text-muted" style="font-size:.73rem">
              Sales, Purchases, Expenses, Cheques, Stock Movements, Customer Payments.
              Keeps: Products, Customers, Suppliers, Users, Settings.
            </div>
          </div>
          <i class="bi bi-chevron-right text-danger ms-auto mt-1"></i>
        </a>

        <a href="<?= APP_URL ?>/developer/clear?scope=logs"
           class="d-flex align-items-start gap-3 p-3 rounded text-decoration-none"
           style="background:#fffbeb;border:1px solid #fde68a">
          <i class="bi bi-journal-x text-warning fs-4 flex-shrink-0 mt-1"></i>
          <div>
            <div class="fw-bold text-warning small">Clear Activity Logs Only</div>
            <div class="text-muted" style="font-size:.73rem">
              Removes all activity log entries. No transactional data affected.
            </div>
          </div>
          <i class="bi bi-chevron-right text-warning ms-auto mt-1"></i>
        </a>

        <a href="<?= APP_URL ?>/developer/clear?scope=full_reset"
           class="d-flex align-items-start gap-3 p-3 rounded text-decoration-none"
           style="background:#fff7ed;border:1px solid #fdba74">
          <i class="bi bi-arrow-counterclockwise text-orange fs-4 flex-shrink-0 mt-1"
             style="color:#ea580c"></i>
          <div>
            <div class="fw-bold small" style="color:#ea580c">Full Reset — Wipe & Reset IDs</div>
            <div class="text-muted" style="font-size:.73rem">
              Deletes: Transactions, Products, Branches.
              Resets to 0: Customer & Supplier balances (records kept).
              Resets ALL table IDs back to 1.
            </div>
          </div>
          <i class="bi bi-chevron-right ms-auto mt-1" style="color:#ea580c"></i>
        </a>

        <a href="<?= APP_URL ?>/developer/clear?scope=full"
           class="d-flex align-items-start gap-3 p-3 rounded text-decoration-none"
           style="background:#1e293b;border:1px solid #334155">
          <i class="bi bi-nuclear text-danger fs-4 flex-shrink-0 mt-1" style="animation:pulse 1s infinite"></i>
          <div>
            <div class="fw-bold text-danger small">FULL Clear — All User Data</div>
            <div class="text-muted" style="font-size:.73rem;color:#94a3b8!important">
              Permanently deletes Customers, Suppliers, Products, Returns, Held Bills
              and ALL transactions. Keeps: Users, Roles, Settings, Branches.
              <span class="text-danger fw-bold">IRREVERSIBLE.</span>
            </div>
          </div>
          <i class="bi bi-chevron-right text-danger ms-auto mt-1"></i>
        </a>
      </div>
    </div>
  </div>

</div>

<style>
@keyframes pulse{0%,100%{opacity:1}50%{opacity:.4}}
</style>