<?php

namespace App\Models;

use CodeIgniter\Model;

class AttendanceRecordModel extends Model
{
    protected $table = 'attendance_record';
    protected $primaryKey = 'id';
    protected $allowedFields = [
        'class_section_id',
        'student_id',
        'school_id',
        'total_absence',
        'total_late',
        'total_presence',
        'total_attendance',
        'semester',
        'school_year',
        'modified_by',
        'created_at',
        'updated_at'
    ];

    protected $useTimestamps = true;
    protected $createdField = 'created_at';
    protected $updatedField = 'updated_at';

    /**
     * Get attendance records by class section, semester, and school year.
     *
     * @param int $classSectionId
     * @param string $semester
     * @param string $school_year
     * @return array
     */
    public function getAttendanceRecordByClass($classSectionId, $semester, $school_year)
    {
        return $this->where([
            'class_section_id' => $classSectionId,
            'semester' => $semester,
            'school_year' => $school_year
        ])->findAll();
    }

    /**
     * Get attendance record for a specific student in a class section.
     *
     * @param int $student_id
     * @param int $classSectionId
     * @param string $semester
     * @param string $school_year
     * @return array|null
     */
    public function getAttendanceRecord($student_id, $classSectionId, $semester, $school_year)
    {
        return $this->where([
            'student_id' => $student_id,
            'class_section_id' => $classSectionId,
            'semester' => $semester,
            'school_year' => $school_year
        ])->first();
    }

    /**
     * Update an existing attendance record.
     *
     * @param int $attendanceId
     * @param array $data
     * @return bool
     */
    public function updateAttendanceRecord($attendanceId, $data)
    {
        return $this->update($attendanceId, $data);
    }

    /**
     * Increment attendance counters for a student.
     *
     * @param int $student_id
     * @param int $classSectionId
     * @param string $semester
     * @param string $school_year
     * @param array $increments (e.g., ['total_absence' => 1, 'total_presence' => 0])
     * @return bool
     */
    public function incrementAttendanceCounters($student_id, $classSectionId, $semester, $school_year, $increments)
    {
        $record = $this->getAttendanceRecord($student_id, $classSectionId, $semester, $school_year);

        if ($record) {
            foreach ($increments as $field => $value) {
                if (isset($record[$field])) {
                    $record[$field] += $value;
                }
            }
            return $this->update($record['id'], $record);
        }

        return false;
    }

    public function getTotalAbsences($studentId, $semester, $schoolYear)
    {
        $records = $this->where('student_id', $studentId)
            ->where('semester', $semester)
            ->where('school_year', $schoolYear)
            ->findAll();

        if (empty($records)) {
            return 0;
        }

        $totalAbsences = 0;
        foreach ($records as $record) {
            $totalAbsences += (int) ($record['total_absence'] ?? 0);
        }

        return $totalAbsences;
    }

}
