<?php
error_reporting(E_ALL);
ini_set('display_errors', 0);
header('Content-Type: application/json');

require_once 'config.php';

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    try {
        $db = getDB();
        $stmt = $db->query('
            SELECT
                m.id,
                m.name,
                m.latitude,
                m.longitude,
                m.radius_meters,
                m.created_at,
                m.updated_at,
                COUNT(np.id) AS need_point_count
            FROM masjids m
            LEFT JOIN need_points np ON np.masjid_id = m.id
            GROUP BY m.id
            ORDER BY m.created_at DESC
        ');

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

        echo json_encode([
            'success' => true,
            'masjids' => $masjids
        ]);
    } catch (Exception $e) {
        http_response_code(500);
        echo json_encode(['error' => $e->getMessage()]);
    }
} else {
    http_response_code(405);
    echo json_encode(['error' => 'Method not allowed']);
}
?>
