<?php
if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Incidentreport_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Get all incident reports with joined details
     */
    public function get_all_incidents()
    {
        $this->db->select('
            incident_reports.id,
            students.firstname,
            students.lastname,
            students.gender,
            students.dob,
            classes.class,
            sections.section,
            incident_types.name AS incident_type,
            incident_categories.name AS incident_category,
            incident_reports.reported_to,
            incident_reports.reported_by,
            incident_reports.description,
            incident_reports.action_taken,
            incident_reports.outcome,
            incident_reports.created_at
        ');
        $this->db->from('incident_reports');
        $this->db->join('students', 'students.id = incident_reports.student_id', 'left');
        $this->db->join('classes', 'classes.id = incident_reports.class_id', 'left');
        $this->db->join('sections', 'sections.id = incident_reports.section_id', 'left');
        $this->db->join('incident_types', 'incident_types.id = incident_reports.incident_type_id', 'left');
        $this->db->join('incident_categories', 'incident_categories.id = incident_reports.incident_category_id', 'left');
        $this->db->order_by('incident_reports.id', 'DESC');

        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * Get all distinct DOBs (used for DOB filter)
     */
    public function getAllDobs()
    {
        $this->db->select('DISTINCT dob', false);
        $this->db->from('students');
        $this->db->where('dob IS NOT NULL');
        $this->db->order_by('dob', 'ASC');
        $query = $this->db->get();

        // Return an array of DOBs in yyyy-mm-dd format
        return array_column($query->result_array(), 'dob');
    }

    /**
     * Get single incident by ID
     */
    public function get_incident($id)
    {
        $this->db->select('
            incident_reports.id,
            students.firstname,
            students.lastname,
            students.gender,
            students.dob,
            classes.class,
            sections.section,
            incident_types.name AS incident_type,
            incident_categories.name AS incident_category,
            incident_reports.reported_to,
            incident_reports.reported_by,
            incident_reports.description,
            incident_reports.action_taken,
            incident_reports.outcome,
            incident_reports.created_at
        ');
        $this->db->from('incident_reports');
        $this->db->join('students', 'students.id = incident_reports.student_id', 'left');
        $this->db->join('classes', 'classes.id = incident_reports.class_id', 'left');
        $this->db->join('sections', 'sections.id = incident_reports.section_id', 'left');
        $this->db->join('incident_types', 'incident_types.id = incident_reports.incident_type_id', 'left');
        $this->db->join('incident_categories', 'incident_categories.id = incident_reports.incident_category_id', 'left');
        $this->db->where('incident_reports.id', $id);

        $query = $this->db->get();
        return $query->row_array();
    }
}
