<?php
    
    include('../config.php');

    try {        

        // Create PDO connection
        $pdo = new PDO($dsn, $username, $password);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        // Query to check for the latest update
        $sql = "SELECT MAX(timestamp) as latest_update FROM updates";
        $stmt = $pdo->query($sql);

        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($row) {
            echo json_encode(['latest_update' => $row['latest_update']]);
        } else {
            echo json_encode(['latest_update' => null]);
        }
    } catch (PDOException $e) {
        echo json_encode(['error' => 'Query failed: ' . $e->getMessage()]);
    } catch (Exception $e) {
        echo json_encode(['error' => 'Unexpected error: ' . $e->getMessage()]);
    }
?>
