<?php
/**
 * Counter - Counting Instructions
 *
 * Allows the counter to review specific counting instructions
 * to ensure adherence to protocols set by the admin.
 */
require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/../middleware/role_middleware.php';
counter_only();

$pageTitle = 'Counting Instructions';
$db = Database::getInstance();

// Filter by session
$sessionFilter = $_GET['session_id'] ?? '';

$where = "ci.status = 'active'";
$params = [];
if ($sessionFilter) {
    $where .= " AND (ci.session_id = ? OR ci.session_id IS NULL)";
    $params[] = $sessionFilter;
}

$instructions = $db->fetchAll("
    SELECT ci.*, u.full_name as creator_name, s.name as session_name
    FROM counting_instructions ci
    LEFT JOIN users u ON ci.created_by = u.id
    LEFT JOIN sessions s ON ci.session_id = s.id
    WHERE $where
    ORDER BY ci.session_id IS NULL ASC, ci.created_at DESC
", $params);

$sessions = $db->fetchAll("
    SELECT DISTINCT s.id, s.name
    FROM sessions s
    JOIN counting_instructions ci ON ci.session_id = s.id
    WHERE ci.status = 'active'
    ORDER BY s.name
");

include APP_ROOT . '/templates/header.php';
?>

<div class="toolbar">
    <form method="GET" class="toolbar-left" style="display:flex;gap:10px;flex-wrap:wrap">
        <select name="session_id" class="form-control" style="width:auto">
            <option value="">All Instructions</option>
            <option value="general" <?= $sessionFilter==='general'?'selected':'' ?>>General Only</option>
            <?php foreach ($sessions as $s): ?>
            <option value="<?= $s['id'] ?>" <?= $sessionFilter==$s['id']?'selected':'' ?>><?= e($s['name']) ?></option>
            <?php endforeach; ?>
        </select>
        <button type="submit" class="btn btn-outline">Filter</button>
    </form>
</div>

<?php if (empty($instructions)): ?>
<div class="card">
    <div class="empty-state">
        <div class="icon">&#128221;</div>
        <h3>No Instructions Available</h3>
        <p>No active counting instructions have been published. Check back later or contact your admin.</p>
    </div>
</div>
<?php else: ?>
<?php foreach ($instructions as $inst): ?>
<div class="card mb-2">
    <div class="card-header">
        <div>
            <h3><?= e($inst['title']) ?></h3>
            <small class="text-muted">
                <?php if ($inst['session_name']): ?>
                    Session: <strong><?= e($inst['session_name']) ?></strong> |
                <?php else: ?>
                    <strong>General</strong> |
                <?php endif; ?>
                Published <?= format_datetime($inst['created_at']) ?>
                <?php if ($inst['creator_name']): ?>
                    by <?= e($inst['creator_name']) ?>
                <?php endif; ?>
            </small>
        </div>
        <?= status_badge($inst['status']) ?>
    </div>
    <div class="card-body">
        <div style="white-space:pre-wrap;line-height:1.8;font-size:15px"><?= nl2br(e($inst['content'])) ?></div>
    </div>
</div>
<?php endforeach; ?>
<?php endif; ?>

<?php include APP_ROOT . '/templates/footer.php'; ?>
