<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class StudentMedicalCondition extends BaseModel
{
    protected $table = 'student_medical_conditions';

    /**
     * CI: timestamps disabled
     */
    public $timestamps = false;

    protected $fillable = [
        'student_id',
        'condition_name',
    ];

    protected $casts = [
        'student_id' => 'integer',
    ];

    /* ============================================================
     * Relationships (optional)
     * ============================================================
     */

    public function student(): BelongsTo
    {
        return $this->belongsTo(Student::class, 'student_id');
    }

    /* ============================================================
     * Scopes
     * ============================================================
     */

    public function scopeForStudent(Builder $q, int $studentId): Builder
    {
        return $q->where('student_id', $studentId);
    }

    public function scopeForStudents(Builder $q, array $studentIds): Builder
    {
        $ids = array_values(array_unique(array_filter(array_map('intval', $studentIds))));
        return empty($ids) ? $q->whereRaw('1=0') : $q->whereIn('student_id', $ids);
    }

    /* ============================================================
     * CI-compatible methods
     * ============================================================
     */

    /**
     * Get all medical conditions for a given student.
     * CI: getMedicalByStudentId()
     */
    public static function getMedicalByStudentId(int $studentId): array
    {
        return static::query()
            ->forStudent($studentId)
            ->orderBy('id', 'asc')
            ->get()
            ->all();
    }

    /**
     * Get medical conditions for multiple students, grouped by student_id.
     * CI: getMedicalByStudentIds()
     */
    public static function getMedicalByStudentIds(array $studentIds): array
    {
        $rows = static::query()
            ->forStudents($studentIds)
            ->orderBy('student_id', 'asc')
            ->orderBy('id', 'asc')
            ->get();

        $grouped = [];
        foreach ($rows as $row) {
            $sid = (int) $row->student_id;
            $grouped[$sid][] = $row;
        }

        return $grouped;
    }

    /* ============================================================
     * Validation helper (FormRequest/controller)
     * Mirrors CI rules/messages
     * ============================================================
     */

    public static function rules(bool $updating = false): array
    {
        $req = $updating ? 'sometimes' : 'required';

        return [
            'student_id'     => [$req, 'integer', 'min:1', 'exists:students,id'],
            'condition_name' => [$req, 'string', 'max:100'],
        ];
    }

    public static function messages(): array
    {
        return [
            'student_id.required'     => 'Student ID is required.',
            'student_id.min'          => 'Invalid Student ID.',
            'condition_name.required' => 'Condition name is required.',
            'condition_name.max'      => 'Condition name must be 100 characters or fewer.',
        ];
    }
}