<?php
require_once 'WorkflowEngine.php';

session_start();

header('Content-Type: application/json');

$response = ['success' => false, 'message' => 'An error occurred.', 'attendance' => []];

if (!isset($_SESSION['user_id'])) {
    $response['message'] = 'You must be logged in to perform this action.';
    echo json_encode($response);
    exit;
}

$groupId = $_GET['group_id'] ?? null;
$meetingDate = $_GET['meeting_date'] ?? null;

if (!$groupId || !$meetingDate) {
    $response['message'] = 'Missing required parameters.';
    echo json_encode($response);
    exit;
}

try {
    $workflowEngine = new WorkflowEngine();
    $attendance = $workflowEngine->getMeetingAttendanceByGroupAndDate((int)$groupId, $meetingDate);
    $response['success'] = true;
    $response['attendance'] = $attendance;
} catch (Exception $e) {
    error_log($e->getMessage());
    $response['message'] = 'Error fetching attendance: ' . $e->getMessage();
}

echo json_encode($response);
