<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/../includes/auth.php';
requireLogin();


require_once __DIR__ . '/../config/database.php';

/* ==============================
   DASHBOARD COUNTS (STRICT MULTI-TENANT)
============================== */

$isSuperAdmin = ($_SESSION['role_name'] === 'SUPER_ADMIN');
$companyId = $_SESSION['company_id'] ?? null;

if ($isSuperAdmin) {

    $totalMeters = $pdo->query("SELECT COUNT(*) FROM meters")->fetchColumn();
    $inStock = $pdo->query("SELECT COUNT(*) FROM meters WHERE status = 'IN_STOCK'")->fetchColumn();
    $withDistributor = $pdo->query("SELECT COUNT(*) FROM meters WHERE status = 'WITH_DISTRIBUTOR'")->fetchColumn();
    $installed = $pdo->query("SELECT COUNT(*) FROM meters WHERE status = 'INSTALLED'")->fetchColumn();
    $pendingRegistrations = $pdo->query("SELECT COUNT(*) FROM meter_registrations WHERE status = 'SUBMITTED'")->fetchColumn();
    $pendingReplacements = $pdo->query("SELECT COUNT(*) FROM replacement_requests WHERE status = 'PENDING'")->fetchColumn();

} else {

    $stmt = $pdo->prepare("SELECT COUNT(*) FROM meters WHERE company_id = ?");
    $stmt->execute([$companyId]);
    $totalMeters = $stmt->fetchColumn();

    $stmt = $pdo->prepare("SELECT COUNT(*) FROM meters WHERE company_id = ? AND status = 'IN_STOCK'");
    $stmt->execute([$companyId]);
    $inStock = $stmt->fetchColumn();

    $stmt = $pdo->prepare("SELECT COUNT(*) FROM meters WHERE company_id = ? AND status = 'WITH_DISTRIBUTOR'");
    $stmt->execute([$companyId]);
    $withDistributor = $stmt->fetchColumn();

    $stmt = $pdo->prepare("SELECT COUNT(*) FROM meters WHERE company_id = ? AND status = 'INSTALLED'");
    $stmt->execute([$companyId]);
    $installed = $stmt->fetchColumn();

    $stmt = $pdo->prepare("SELECT COUNT(*) FROM meter_registrations WHERE company_id = ? AND status = 'SUBMITTED'");
    $stmt->execute([$companyId]);
    $pendingRegistrations = $stmt->fetchColumn();

    $stmt = $pdo->prepare("SELECT COUNT(*) FROM replacement_requests WHERE company_id = ? AND status = 'PENDING'");
    $stmt->execute([$companyId]);
    $pendingReplacements = $stmt->fetchColumn();
}

require_once __DIR__ . '/../includes/layout.php';
?>
<?php if ($_SESSION['role_name'] === 'INSTALLER'): ?>
    <?php require __DIR__ . '/installer_dashboard.php'; ?>
<?php else: ?>

<div class="page-header">
    <h2>Welcome, <?= htmlspecialchars($_SESSION['full_name']); ?></h2>
    <p class="page-subtitle">
        EDM Core System • National Infrastructure Control Panel
    </p>
</div>

<div class="dashboard-grid">

    <div class="stat-card orange">
        <h4>Total Meters</h4>
        <div class="number"><?= $totalMeters ?></div>
    </div>

    <div class="stat-card green">
        <h4>In Stock</h4>
        <div class="number"><?= $inStock ?></div>
    </div>

    <div class="stat-card blue">
        <h4>With Distributor</h4>
        <div class="number"><?= $withDistributor ?></div>
    </div>

    <div class="stat-card red">
        <h4>Installed</h4>
        <div class="number"><?= $installed ?></div>
    </div>

    <div class="stat-card orange">
        <h4>Pending Registrations</h4>
        <div class="number"><?= $pendingRegistrations ?></div>
    </div>

    <div class="stat-card red">
        <h4>Pending Replacements</h4>
        <div class="number"><?= $pendingReplacements ?></div>
    </div>

</div>
<?php endif; ?>

<?php require_once __DIR__ . '/../includes/layout_footer.php'; ?>
