<?php

namespace App\Models;

use CodeIgniter\Model;

class ClassAttendanceModel extends Model
{
    protected $table = 'class_attendance';

    protected $primaryKey = 'id';

    protected $allowedFields = [
        'class_id',
        'student_id',
        'class_diary_id',
        'date',
        'duration',
        'status',
        'comment',
        'attendance_status',
    ];

    public function getAttendanceSummary($studentId)
    {
        return $this->select("
            SUM(CASE WHEN attendance_status = 1 THEN 1 ELSE 0 END) AS present_count,
            SUM(CASE WHEN attendance_status = 0 THEN 1 ELSE 0 END) AS absent_count,
            COUNT(*) AS total_classes
        ", false)
            ->where('student_id', $studentId)
            ->first();
    }

    //protected $useTimestamps = true;
}