<?php
session_start();
require_once '../config/database.php';
header('Content-Type: application/json');

$db = new Database();
$conn = $db->getConnection();

$user_id = $_SESSION['user_id'];
$gereja_id_session = $_SESSION['gereja_id'];

// AMBIL LANGSUNG DARI DATABASE, JANGAN ANDALIN SESSION
$sql = "SELECT g.id, g.nama_gereja, g.alamat 
        FROM users u 
        JOIN gereja g ON u.gereja_id = g.id 
        WHERE u.id = ?";
$stmt = $conn->prepare($sql);
$stmt->execute([$user_id]);
$data = $stmt->fetch(PDO::FETCH_ASSOC);

if($data){
    // UPDATE SESSION BIAR GA ERROR LAGI
    $_SESSION['gereja_id'] = $data['id'];
    echo json_encode(['status'=>'success', 'data'=>$data]);
} else {
    echo json_encode(['status'=>'error', 'message'=>'Gereja tidak ditemukan']);
}
?>