<?php
/**
 * Checker - Checking Guidelines
 *
 * Displays checking guidelines that guide the Checker's verification process.
 */
require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/../middleware/role_middleware.php';
checker_only();

$pageTitle = 'Checking Guidelines';
$db = Database::getInstance();

// Fetch active guidelines
$guidelines = $db->fetchAll("
    SELECT cg.*, u.full_name as created_by_name, s.name as session_name
    FROM checking_guidelines cg
    JOIN users u ON cg.created_by = u.id
    LEFT JOIN sessions s ON cg.session_id = s.id
    WHERE cg.status = 'active'
    ORDER BY cg.created_at DESC
");

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

<?php if (empty($guidelines)): ?>
<div class="card">
    <div class="card-body">
        <div class="empty-state">
            <div class="icon">&#128203;</div>
            <h3>No Guidelines Available</h3>
            <p>Checking guidelines have not been published yet. Contact your administrator.</p>
        </div>
    </div>
</div>
<?php else: ?>
    <?php foreach ($guidelines as $guideline): ?>
    <div class="card mb-3">
        <div class="card-header">
            <h3><?= e($guideline['title']) ?></h3>
            <div>
                <?php if ($guideline['session_name']): ?>
                <span class="badge badge-info"><?= e($guideline['session_name']) ?></span>
                <?php endif; ?>
                <small class="text-muted">Updated <?= format_datetime($guideline['updated_at']) ?></small>
            </div>
        </div>
        <div class="card-body">
            <div style="white-space:pre-wrap;line-height:1.7"><?= nl2br(e($guideline['content'])) ?></div>
        </div>
        <div class="card-footer" style="font-size:12px;color:var(--text-muted)">
            Created by <?= e($guideline['created_by_name']) ?> on <?= format_datetime($guideline['created_at']) ?>
        </div>
    </div>
    <?php endforeach; ?>
<?php endif; ?>

<!-- Checker Workflow Reference -->
<div class="card mt-3">
    <div class="card-header">
        <h3>Checker Workflow Reference</h3>
    </div>
    <div class="card-body" style="line-height:1.8">
        <h4 style="margin:0 0 12px">Approval Process</h4>
        <ol style="padding-left:20px;margin-bottom:20px">
            <li>Select a pending count from the <a href="<?= APP_URL ?>/checker/pending_counts.php">Pending Reviews</a> list</li>
            <li>Compare the <strong>System Quantity</strong> against the <strong>Counted Quantity</strong></li>
            <li>Review any notes or observations from the Counter</li>
            <li>Make your decision:
                <ul style="padding-left:20px;margin-top:4px">
                    <li><strong>Approve</strong> &mdash; Count is accurate. Data is locked for reporting.</li>
                    <li><strong>Reject</strong> &mdash; Discrepancy found. Provide comments explaining the issue. The task is returned to the Counter for recount.</li>
                </ul>
            </li>
        </ol>

        <h4 style="margin:0 0 12px">Decision Guidelines</h4>
        <ul style="padding-left:20px;margin-bottom:0">
            <li><strong>Zero variance</strong> &mdash; Always approve unless you suspect data entry fraud</li>
            <li><strong>Minor variance</strong> &mdash; Review context. Small differences may be acceptable depending on item type</li>
            <li><strong>Significant variance</strong> &mdash; Reject with detailed comments. Specify what needs to be rechecked</li>
            <li><strong>Always provide comments when rejecting</strong> &mdash; Guide the Counter on what to look for</li>
        </ul>
    </div>
</div>

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