<?php
// /public_html/status.php
require 'config/db_connect.php';

// Check overall health
$overall_status = "All Systems Operational";
$header_bg = "bg-success";
$icon = "bi-check-circle-fill";

$check = $conn->query("SELECT status FROM system_status WHERE status != 'operational'");
if ($check->num_rows > 0) {
    $overall_status = "Some Systems Experiencing Issues";
    $header_bg = "bg-warning text-dark";
    $icon = "bi-exclamation-triangle-fill";
    
    // Check for outage
    $check_outage = $conn->query("SELECT status FROM system_status WHERE status = 'outage'");
    if ($check_outage->num_rows > 0) {
        $overall_status = "Service Outage Detected";
        $header_bg = "bg-danger";
        $icon = "bi-x-octagon-fill";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>System Status - QR SaaS</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
    <style>
        .status-dot { height: 12px; width: 12px; border-radius: 50%; display: inline-block; }
        .dot-operational { background-color: #198754; }
        .dot-degraded { background-color: #ffc107; }
        .dot-outage { background-color: #dc3545; }
    </style>
</head>
<body class="bg-light">

    <div class="<?php echo $header_bg; ?> text-white text-center py-5 mb-5">
        <div class="container">
            <h1 class="display-4 fw-bold"><i class="bi <?php echo $icon; ?>"></i></h1>
            <h2><?php echo $overall_status; ?></h2>
            <p class="lead">Last updated: <?php echo date('H:i T'); ?></p>
        </div>
    </div>

    <div class="container" style="max-width: 800px;">
        
        <div class="card shadow-sm mb-5">
            <div class="card-body p-0">
                <ul class="list-group list-group-flush">
                    <?php
                    $comps = $conn->query("SELECT * FROM system_status");
                    while($c = $comps->fetch_assoc()):
                        $dot = "dot-" . $c['status'];
                        $text_color = ($c['status'] == 'operational') ? 'text-success' : (($c['status'] == 'outage') ? 'text-danger' : 'text-warning');
                    ?>
                    <li class="list-group-item d-flex justify-content-between align-items-center py-3">
                        <span class="fw-bold"><?php echo $c['component_name']; ?></span>
                        <div class="d-flex align-items-center">
                            <span class="<?php echo $text_color; ?> me-2 text-capitalize"><?php echo $c['status']; ?></span>
                            <span class="status-dot <?php echo $dot; ?>"></span>
                        </div>
                    </li>
                    <?php endwhile; ?>
                </ul>
            </div>
        </div>

        <h4 class="mb-3">Past Incidents</h4>
        <?php
        $incidents = $conn->query("SELECT * FROM incidents ORDER BY created_at DESC LIMIT 10");
        if ($incidents->num_rows > 0):
            while($inc = $incidents->fetch_assoc()):
                $color = ($inc['status'] == 'resolved') ? 'success' : 'warning';
        ?>
            <div class="card mb-3 border-start border-4 border-<?php echo $color; ?>">
                <div class="card-body">
                    <h5 class="card-title"><?php echo htmlspecialchars($inc['title']); ?></h5>
                    <h6 class="card-subtitle mb-2 text-muted">
                        <?php echo date('M d, Y - H:i', strtotime($inc['created_at'])); ?> 
                        &bull; <span class="badge bg-<?php echo $color; ?>"><?php echo strtoupper($inc['status']); ?></span>
                    </h6>
                    <p class="card-text"><?php echo nl2br(htmlspecialchars($inc['message'])); ?></p>
                </div>
            </div>
        <?php endwhile; else: ?>
            <div class="text-center text-muted py-4">No incidents reported recently.</div>
        <?php endif; ?>

        <div class="text-center mt-5 mb-5">
            <a href="index.php" class="text-decoration-none">&larr; Back to Website</a>
        </div>
    </div>

</body>
</html>