<?php
/**
 * Workflow State Validation Middleware
 */

function validate_session_status(string $requiredStatus): bool
{
    if (!isset($_GET['session_id']) && !isset($_POST['session_id'])) {
        return false;
    }

    $sessionId = $_GET['session_id'] ?? $_POST['session_id'];
    $db = Database::getInstance();
    $session = $db->fetch("SELECT status FROM sessions WHERE id = ?", [$sessionId]);

    return $session && $session['status'] === $requiredStatus;
}

function validate_assignment_ownership(int $assignmentId, int $userId): bool
{
    $db = Database::getInstance();
    $assignment = $db->fetch("SELECT counter_id FROM assignments WHERE id = ?", [$assignmentId]);
    return $assignment && (int) $assignment['counter_id'] === $userId;
}

function validate_assignment_status(int $assignmentId, array $allowedStatuses): bool
{
    $db = Database::getInstance();
    $assignment = $db->fetch("SELECT status FROM assignments WHERE id = ?", [$assignmentId]);
    return $assignment && in_array($assignment['status'], $allowedStatuses);
}
