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

class Student_model extends CI_Model {

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

    /**
     * Get the student's profile details including class level
     *
     * @param int $user_id
     * @return object|bool
     */
    public function get_student_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 a student is enrolled in
     *
     * @param int $student_id
     * @return array
     */
    public function get_student_subjects($student_id) {
        $student = $this->db->get_where('users', ['id' => $student_id])->row();
        if (!$student || empty($student->class_level)) return [];
        
        $this->db->select('s.id, s.code, s.name, s.credit_hours, u.full_name as teacher_name');
        $this->db->from('classes c');
        $this->db->join('teacher_subjects ts', 'ts.class_id = c.id');
        $this->db->join('subjects s', 's.id = ts.subject_id');
        $this->db->join('users u', 'u.id = ts.teacher_id', 'left');
        $this->db->where('c.name', $student->class_level);
        
        $query = $this->db->get();
        return $query->result();
    }

    /**
     * Get the student's fee details
     *
     * @param int $student_id
     * @return array
     */
    public function get_student_fees($student_id) {
        $this->db->select('student_fees.*, fee_structures.description, fee_structures.amount as total_amount');
        $this->db->from('student_fees');
        $this->db->join('fee_structures', 'fee_structures.id = student_fees.fee_structure_id');
        $this->db->where('student_fees.student_id', $student_id);
        $query = $this->db->get();
        return $query->result();
    }

    /**
     * Get recent available books from the library
     *
     * @param int $limit
     * @return array
     */
    public function get_recent_books($limit = 5) {
        $this->db->select('id, title, author, copies_available, cover_image, subject');
        $this->db->from('books');
        $this->db->order_by('id', 'DESC');
        $this->db->limit($limit);
        $query = $this->db->get();
        return $query->result();
    }

    // --- CURRICULUM ---

    public function get_subject_details($subject_id, $student_id) {
        $student = $this->db->get_where('users', ['id' => $student_id])->row();
        if (!$student || empty($student->class_level)) return false;
        
        $this->db->select('ts.class_id, s.name as subject_name, c.name as class_name');
        $this->db->from('classes c');
        $this->db->join('teacher_subjects ts', 'ts.class_id = c.id');
        $this->db->join('subjects s', 's.id = ts.subject_id');
        $this->db->where('c.name', $student->class_level);
        $this->db->where('s.id', $subject_id);
        return $this->db->get()->row();
    }

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

        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', 'ASC');
            $topic->exercises = $this->db->get('exercises')->result();
        }

        return $topics;
    }
}
