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

class Admin extends CI_Controller {

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

    public function index() {
        redirect('admin/dashboard');
    }

    public function dashboard() {
        $data['total_students'] = $this->Admin_model->get_total_students();
        $data['total_teachers'] = $this->Admin_model->get_total_teachers();
        $data['total_subjects'] = $this->Admin_model->get_total_subjects();
        $data['total_classes'] = $this->Admin_model->get_total_classes();
        
        $this->load->view('admin/dashboard', $data);
    }

    // --- SUBJECTS MANAGEMENT ---

    public function subjects() {
        $data['subjects'] = $this->Admin_model->get_subjects();
        $this->load->view('admin/subjects', $data);
    }

    public function add_subject() {
        if ($this->input->post()) {
            $data = [
                'code' => $this->input->post('code', true),
                'name' => $this->input->post('name', true),
                'description' => $this->input->post('description', true),
                'credit_hours' => $this->input->post('credit_hours', true)
            ];

            if ($this->Admin_model->add_subject($data)) {
                $this->session->set_flashdata('success', 'Subject added successfully.');
            } else {
                $this->session->set_flashdata('error', 'Failed to add subject. The subject code might already exist.');
            }
        }
        redirect('admin/subjects');
    }

    // --- ASSIGNMENTS MANAGEMENT ---

    public function assignments() {
        $data['teachers'] = $this->Admin_model->get_teachers();
        $data['subjects'] = $this->Admin_model->get_subjects();
        $data['classes'] = $this->Admin_model->get_classes();
        $data['assignments'] = $this->Admin_model->get_assignments();
        
        $this->load->view('admin/assignments', $data);
    }

    public function add_assignment() {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $data = [
                'teacher_id' => $this->input->post('teacher_id'),
                'subject_id' => $this->input->post('subject_id'),
                'class_id' => $this->input->post('class_id')
            ];
            
            if ($this->Admin_model->assign_teacher_to_class($data['teacher_id'], $data['subject_id'], $data['class_id'])) {
                $this->session->set_flashdata('success', 'Teacher assigned successfully!');
            } else {
                $this->session->set_flashdata('error', 'Failed to assign teacher or assignment already exists.');
            }
            redirect('admin/assignments');
        }
    }

    // --- TEACHER MANAGEMENT ---

    public function teachers() {
        $data['teachers'] = $this->Admin_model->get_teachers();
        $this->load->view('admin/teachers', $data);
    }

    public function add_teacher() {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $data = [
                'full_name' => $this->input->post('full_name'),
                'username' => $this->input->post('username'),
                'email' => $this->input->post('email'),
                'password' => $this->input->post('password'),
                'phone' => $this->input->post('phone')
            ];
            
            if ($this->Admin_model->insert_teacher($data)) {
                $this->session->set_flashdata('success', 'Teacher created successfully!');
            } else {
                $this->session->set_flashdata('error', 'Failed to create teacher. That username might already be in use.');
            }
            redirect('admin/teachers');
        }
    }
}
