<?php
/**
 * Shared Grading Functions for Report Generation
 * This file contains common functions used by both PDF generation scripts
 */

// Function to get grade from grading scales - shared between all report scripts
function getGradeFromScale($mark, $subject_id, $class_id, $academic_year, $term, $school_id, $connection) {
    $grade_info = ['grade' => '', 'remarks' => ''];
    
    if ($mark === null) return $grade_info;
    
    try {
        $term_variations = [
            $term,
            strtoupper($term),
            strtolower($term),
            ucfirst(strtolower($term)),
            str_replace('TERM ', 'Term ', $term),
            str_replace('Term ', 'TERM ', $term)
        ];
        
        // Remove duplicates
        $term_variations = array_unique($term_variations);
        
        foreach ($term_variations as $term_variant) {
            // Try subject-specific scale first
            $sql = "SELECT grade, remarks FROM grading_scales 
                   WHERE school_id = ? AND academic_year = ? AND term = ? 
                   AND class_id = ? AND subject_id = ? 
                   AND ? BETWEEN min_mark AND max_mark 
                   ORDER BY min_mark DESC LIMIT 1";
            
            $stmt = $connection->prepare($sql);
            $stmt->bind_param("issiid", $school_id, $academic_year, $term_variant, $class_id, $subject_id, $mark);
            $stmt->execute();
            $result = $stmt->get_result();
            
            if ($result && $result->num_rows > 0) {
                $grade_info = $result->fetch_assoc();
                return $grade_info;
            }
            
            // Try class-specific scale (any subject)
            $sql = "SELECT grade, remarks FROM grading_scales 
                   WHERE school_id = ? AND academic_year = ? AND term = ? 
                   AND class_id = ? AND subject_id IS NULL 
                   AND ? BETWEEN min_mark AND max_mark 
                   ORDER BY min_mark DESC LIMIT 1";
            
            $stmt = $connection->prepare($sql);
            $stmt->bind_param("issid", $school_id, $academic_year, $term_variant, $class_id, $mark);
            $stmt->execute();
            $result = $stmt->get_result();
            
            if ($result && $result->num_rows > 0) {
                $grade_info = $result->fetch_assoc();
                return $grade_info;
            }
            
            // Try general scale (any class, any subject)
            $sql = "SELECT grade, remarks FROM grading_scales 
                   WHERE school_id = ? AND academic_year = ? AND term = ? 
                   AND class_id IS NULL AND subject_id IS NULL 
                   AND ? BETWEEN min_mark AND max_mark 
                   ORDER BY min_mark DESC LIMIT 1";
            
            $stmt = $connection->prepare($sql);
            $stmt->bind_param("issd", $school_id, $academic_year, $term_variant, $mark);
            $stmt->execute();
            $result = $stmt->get_result();
            
            if ($result && $result->num_rows > 0) {
                $grade_info = $result->fetch_assoc();
                return $grade_info;
            }
        }
        
        // If no specific scale found, use default grading
        if ($mark >= 70) {
            $grade_info = ['grade' => 'A', 'remarks' => 'Excellent'];
        } elseif ($mark >= 60) {
            $grade_info = ['grade' => 'B', 'remarks' => 'Good'];
        } elseif ($mark >= 50) {
            $grade_info = ['grade' => 'C', 'remarks' => 'Average'];
        } elseif ($mark >= 40) {
            $grade_info = ['grade' => 'D', 'remarks' => 'Below Average'];
        } else {
            $grade_info = ['grade' => 'F', 'remarks' => 'Fail'];
        }
        
    } catch (Exception $e) {
        // Fallback grading in case of database error
        if ($mark >= 70) {
            $grade_info = ['grade' => 'A', 'remarks' => 'Excellent'];
        } elseif ($mark >= 60) {
            $grade_info = ['grade' => 'B', 'remarks' => 'Good'];
        } elseif ($mark >= 50) {
            $grade_info = ['grade' => 'C', 'remarks' => 'Average'];
        } elseif ($mark >= 40) {
            $grade_info = ['grade' => 'D', 'remarks' => 'Below Average'];
        } else {
            $grade_info = ['grade' => 'F', 'remarks' => 'Fail'];
        }
    }
    
    return $grade_info;
}
?>