<?php
require_once __DIR__ . '/../../includes/auth.php';
requireLogin();
requirePermission('inventory.manage');

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

$user_id    = $_SESSION['user_id'] ?? 0;
$company_id = $_SESSION['company_id'] ?? null; // kept for compatibility
$role       = $_SESSION['role_name'] ?? '';

/* ===============================
   Fetch Operational Meters (UPDATED TO BILLING)
================================= */

if ($role === 'SUPER_ADMIN') {

    $stmt = $pdo->query("
        SELECT 
            om.*,
            ba.account_name,
            bc.customer_name
        FROM operational_meters om
        LEFT JOIN billing_accounts ba ON om.account_id = ba.id
        LEFT JOIN billing_customers bc ON ba.customer_id = bc.id
        ORDER BY om.id DESC
    ");

    $meters = $stmt->fetchAll(PDO::FETCH_ASSOC);

} else {

    // TEMP: still using company_id session (we will upgrade later)
    $stmt = $pdo->prepare("
        SELECT 
            om.*,
            ba.account_name,
            bc.customer_name
        FROM operational_meters om
        LEFT JOIN billing_accounts ba ON om.account_id = ba.id
        LEFT JOIN billing_customers bc ON ba.customer_id = bc.id
        ORDER BY om.id DESC
    ");

    $stmt->execute();
    $meters = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

$breadcrumb = "Operational Meters";
require_once __DIR__ . '/../../includes/layout.php';
?>

<div class="container">

    <div class="page-header">
        <h2>Operational Meters</h2>
        <p class="page-subtitle">
            Active commercial meters connected to the operational layer.
        </p>
    </div>

    <div class="topbar">
        <a class="btn secondary" href="/core/modules/inventory/units.php">
            ← Back to Inventory
        </a>
    </div>

    <div class="table-wrap">
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Meter Number</th>
                    <th>Customer / Account</th>
                    <th>Status</th>
                    <th>Activated At</th>
                </tr>
            </thead>

            <tbody>

                <?php if (!$meters): ?>
                    <tr>
                        <td colspan="5">No operational meters found.</td>
                    </tr>
                <?php else: ?>

                    <?php foreach ($meters as $m): ?>

                        <tr>
                            <td><?= htmlspecialchars($m['id']) ?></td>

                            <td>
                                <a href="meter_view.php?id=<?= $m['id'] ?>">
                                    <?= htmlspecialchars($m['meter_number']) ?>
                                </a>
                            </td>

                            <td>
                                <?php if (!empty($m['customer_name']) || !empty($m['account_name'])): ?>
                                    <?= htmlspecialchars($m['customer_name'] ?? 'N/A') ?>
                                    -
                                    <?= htmlspecialchars($m['account_name'] ?? 'N/A') ?>
                                <?php else: ?>
                                    <span style="color:#999;">Unassigned</span>
                                <?php endif; ?>
                            </td>

                            <td>
                                <span class="status <?= htmlspecialchars($m['status']) ?>">
                                    <?= htmlspecialchars($m['status']) ?>
                                </span>
                            </td>

                            <td><?= htmlspecialchars($m['activated_at']) ?></td>
                        </tr>

                    <?php endforeach; ?>

                <?php endif; ?>

            </tbody>

        </table>
    </div>

</div>

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