<?php
declare(strict_types=1);
require_once __DIR__.'/student_projects.php';
function student_project_band(float $score): array {
 if(!is_finite($score)||$score<0||$score>100)throw new InvalidArgumentException('Enter a mark from 0 to 100.');
 return ['letter'=>$score<50?'D':($score<60?'B':($score<70?'B+':($score<80?'A':'A+'))),'outcome'=>$score<50?'Fail':'Pass'];
}
function student_project_score(mixed $value): float {
 if((!is_string($value)&&!is_int($value)&&!is_float($value))||!preg_match('/^\d{1,3}(?:\.\d{1,2})?$/D',(string)$value))throw new InvalidArgumentException('Use a mark from 0 to 100 with up to two decimal places.');
 $score=(float)$value;student_project_band($score);return $score;
}
function student_project_grade(int $id): ?array {
 $q=db()->prepare("SELECT g.*,COALESCE(CASE WHEN LOWER(TRIM(COALESCE(u.full_name,''))) IN ('','null','undefined') THEN NULL ELSE u.full_name END,u.username) AS grader_name FROM student_project_grades g JOIN users u ON u.id=g.graded_by WHERE g.project_id=?");$q->execute([$id]);$grade=$q->fetch();return $grade?array_merge($grade,student_project_band((float)$grade['score'])):null;
}
function student_project_grade_map(array $ids): array {
 if(!$ids)return [];$marks=implode(',',array_fill(0,count($ids),'?'));$q=db()->prepare("SELECT g.*,COALESCE(CASE WHEN LOWER(TRIM(COALESCE(u.full_name,''))) IN ('','null','undefined') THEN NULL ELSE u.full_name END,u.username) AS grader_name FROM student_project_grades g JOIN users u ON u.id=g.graded_by WHERE g.project_id IN ($marks)");$q->execute($ids);$out=[];foreach($q->fetchAll() as $grade)$out[(int)$grade['project_id']]=array_merge($grade,student_project_band((float)$grade['score']));return $out;
}
