<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Teacher_model extends CI_Model {

    public function __construct() {
        $this->load->database();
    }

    /**
     * Get the teacher's profile details
     *
     * @param int $user_id
     * @return object|bool
     */
    public function get_teacher_profile($user_id) {
        $this->db->where('id', $user_id);
        $query = $this->db->get('users');
        if ($query->num_rows() == 1) {
            return $query->row();
        }
        return false;
    }

    /**
     * Get the subjects and classes assigned to this teacher
     *
     * @param int $teacher_id
     * @return array
     */
    public function get_assigned_subjects($teacher_id) {
        $this->db->select('ts.id as assignment_id, s.id as subject_id, s.code, s.name as subject_name, s.credit_hours, c.id as class_id, c.name as class_name');
        $this->db->from('teacher_subjects ts');
        $this->db->join('subjects s', 's.id = ts.subject_id');
        $this->db->join('classes c', 'c.id = ts.class_id');
        $this->db->where('ts.teacher_id', $teacher_id);
        $this->db->order_by('c.name', 'ASC');
        $this->db->order_by('s.name', 'ASC');
        $query = $this->db->get();
        return $query->result();
    }

    // --- CURRICULUM & STUDENTS ---

    public function get_assignment_details($assignment_id, $teacher_id) {
        $this->db->select('ts.id as assignment_id, s.id as subject_id, s.name as subject_name, c.id as class_id, c.name as class_name');
        $this->db->from('teacher_subjects ts');
        $this->db->join('subjects s', 's.id = ts.subject_id');
        $this->db->join('classes c', 'c.id = ts.class_id');
        $this->db->where('ts.id', $assignment_id);
        $this->db->where('ts.teacher_id', $teacher_id);
        return $this->db->get()->row();
    }

    public function get_students_in_class($class_id) {
        $this->db->select('u.id, u.full_name, u.email, cs.admission_number, cs.status');
        $this->db->from('class_students cs');
        $this->db->join('users u', 'u.id = cs.student_id');
        $this->db->where('cs.class_id', $class_id);
        $this->db->where('cs.status', 'active');
        return $this->db->get()->result();
    }

    public function get_topics($subject_id, $class_id, $teacher_id) {
        $this->db->where('subject_id', $subject_id);
        $this->db->where('class_id', $class_id);
        $this->db->where('teacher_id', $teacher_id);
        $this->db->order_by('created_at', 'DESC');
        $topics = $this->db->get('topics')->result();

        // Get exercises and content for each topic
        foreach ($topics as $topic) {
            $this->db->where('topic_id', $topic->id);
            $this->db->order_by('created_at', 'ASC');
            $topic->content = $this->db->get('course_content')->result();

            $this->db->where('topic_id', $topic->id);
            $this->db->order_by('created_at', 'DESC');
            $topic->exercises = $this->db->get('exercises')->result();
        }

        return $topics;
    }

    public function add_topic($data) {
        return $this->db->insert('topics', $data);
    }

    public function add_exercise($data) {
        return $this->db->insert('exercises', $data);
    }

    public function add_content($data) {
        return $this->db->insert('course_content', $data);
    }

    public function update_content($id, $data) {
        $this->db->where('id', $id);
        return $this->db->update('course_content', $data);
    }

    public function delete_content($id) {
        $this->db->where('id', $id);
        return $this->db->delete('course_content');
    }
}
