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

class Student extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        
        // Ensure user is logged in and is a student
        if (!$this->session->userdata('logged_in') || $this->session->userdata('role') !== 'student') {
            redirect('auth/login');
        }
        
        $this->load->model('Student_model');
    }

    public function dashboard() {
        $user_id = $this->session->userdata('user_id');
        
        // Fetch student profile details (including class_level)
        $profile = $this->Student_model->get_student_profile($user_id);
        
        // Fetch subjects
        $subjects = $this->Student_model->get_student_subjects($user_id);
        
        // Fetch fee status
        $fees = $this->Student_model->get_student_fees($user_id);
        
        // Fetch library books
        $books = $this->Student_model->get_recent_books(6);
        
        $data = [
            'profile' => $profile,
            'subjects' => $subjects,
            'fees' => $fees,
            'books' => $books
        ];
        
        $this->load->view('student/dashboard', $data);
    }

    public function subject_details($subject_id) {
        $user_id = $this->session->userdata('user_id');
        $subject = $this->Student_model->get_subject_details($subject_id, $user_id);
        
        if (!$subject) {
            show_404();
        }

        $profile = $this->Student_model->get_student_profile($user_id);
        
        $data['subject'] = $subject;
        $data['profile'] = $profile;
        $data['topics'] = $this->Student_model->get_curriculum($subject_id, $subject->class_id);
        
        $this->load->view('student/subject_details', $data);
    }
}
