<?php

namespace App\Models;

use CodeIgniter\Model;

class StudentMedicalConditionModel extends Model
{
    protected $table            = 'student_medical_conditions';
    protected $primaryKey       = 'id';
    protected $allowedFields    = ['student_id', 'condition_name'];
    public    $timestamps       = false;

    protected $validationRules = [
        'student_id'     => 'required|is_natural_no_zero',
        'condition_name' => 'required|string|max_length[100]',
    ];

    protected $validationMessages = [
        'student_id' => [
            'required'            => 'Student ID is required.',
            'is_natural_no_zero' => 'Invalid Student ID.',
        ],
        'condition_name' => [
            'required'   => 'Condition name is required.',
            'max_length' => 'Condition name must be 100 characters or fewer.',
        ],
    ];

        /**
     * Get all medical conditions for a given student.
     *
     * @param int $studentId
     * @return array
     */
    public function getMedicalByStudentId(int $studentId): array
    {
        return $this->where('student_id', $studentId)
                    ->findAll();
    }   

        /**
     * Get medical conditions for multiple students.
     *
     * @param array $studentIds
     * @return array grouped by student_id
     */
    public function getMedicalByStudentIds(array $studentIds): array
    {
        $rows = $this->whereIn('student_id', $studentIds)->findAll();

        $grouped = [];
        foreach ($rows as $row) {
            $grouped[$row['student_id']][] = $row;
        }

        return $grouped;
    }
}
