<?php

namespace App\Models;

use CodeIgniter\Model;

class JournalAttendanceModel extends Model
{
    protected $table            = 'journal_attendances';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $allowedFields    = [
        'journal_id', 'student_id', 'status',
    ];

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

    /**
     * Ambil semua siswa yang tidak hadir di suatu jurnal, lengkap dengan nama
     */
    public function getAbsenteesByJournal(int $journalId): array
    {
        return $this->select('
                journal_attendances.*,
                students.full_name,
                students.nisn
            ')
            ->join('students', 'students.id = journal_attendances.student_id')
            ->where('journal_attendances.journal_id', $journalId)
            ->findAll();
    }

    /**
     * Hapus semua attendance untuk journal tertentu (saat update)
     */
    public function deleteByJournal(int $journalId): void
    {
        $this->where('journal_id', $journalId)->delete();
    }
}
