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

class Teacher extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper(['url', 'form']);
        
        // Ensure teacher role
        if (!$this->session->userdata('user_id') || $this->session->userdata('role') !== 'teacher') {
            redirect('auth/login');
        }
        
        $this->load->model('Teacher_model');
    }

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

    public function dashboard() {
        $user_id = $this->session->userdata('user_id');
        
        $data['profile'] = $this->Teacher_model->get_teacher_profile($user_id);
        $data['assignments'] = $this->Teacher_model->get_assigned_subjects($user_id);
        
        $this->load->view('teacher/dashboard', $data);
    }

    /* ---------- Modules temporarily disabled until implemented ---------- */
    /*
    public function materials() {
        $data['materials'] = $this->Material_model->get_by_teacher($this->session->userdata('user_id'));
        $this->load->view('teacher/materials', $data);
    }

    public function upload_material() {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $this->Material_model->save($this->input->post(), $_FILES['file']);
            redirect('teacher/materials');
        } else {
            $this->load->view('teacher/upload_material');
        }
    }

    public function assignments() {
        $data['assignments'] = $this->Assignment_model->get_by_teacher($this->session->userdata('user_id'));
        $this->load->view('teacher/assignments', $data);
    }

    public function create_assignment() {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $this->Assignment_model->save($this->input->post(), $_FILES['file'] ?? null);
            redirect('teacher/assignments');
        } else {
            $this->load->view('teacher/create_assignment');
        }
    }

    public function quizzes() {
        $data['quizzes'] = $this->Quiz_model->get_by_teacher($this->session->userdata('user_id'));
        $this->load->view('teacher/quizzes', $data);
    }

    public function create_quiz() {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $this->Quiz_model->save($this->input->post());
            redirect('teacher/quizzes');
        } else {
            $this->load->view('teacher/create_quiz');
        }
    }
    */

    public function class_details($assignment_id) {
        $user_id = $this->session->userdata('user_id');
        $assignment = $this->Teacher_model->get_assignment_details($assignment_id, $user_id);
        
        if (!$assignment) {
            show_404();
        }

        $data['assignment'] = $assignment;
        $data['profile'] = $this->Teacher_model->get_teacher_profile($user_id);
        $data['students'] = $this->Teacher_model->get_students_in_class($assignment->class_id);
        $data['topics'] = $this->Teacher_model->get_topics($assignment->subject_id, $assignment->class_id, $user_id);
        
        $this->load->view('teacher/class_details', $data);
    }

    public function create_topic() {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $data = [
                'subject_id' => $this->input->post('subject_id'),
                'class_id' => $this->input->post('class_id'),
                'teacher_id' => $this->session->userdata('user_id'),
                'title' => $this->input->post('title'),
                'description' => $this->input->post('description')
            ];
            
            $this->Teacher_model->add_topic($data);
            redirect('teacher/class_details/' . $this->input->post('assignment_id'));
        }
    }

    public function create_exercise() {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $data = [
                'topic_id' => $this->input->post('topic_id'),
                'title' => $this->input->post('title'),
                'description' => $this->input->post('description'),
                'due_date' => $this->input->post('due_date')
            ];
            
            $this->Teacher_model->add_exercise($data);
            redirect('teacher/class_details/' . $this->input->post('assignment_id'));
        }
    }

    public function create_content() {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $content_type = $this->input->post('content_type');
            
            $data = [
                'topic_id' => $this->input->post('topic_id'),
                'title' => $this->input->post('title'),
                'content_type' => $content_type,
                'content_text' => $this->input->post('content_text'),
                'term' => $this->input->post('term')
            ];

            if ($content_type === 'youtube') {
                $data['file_path'] = $this->input->post('youtube_url');
            } elseif ($content_type !== 'text') {
                $config['upload_path']          = './uploads/materials/';
                $config['allowed_types']        = 'pdf|mp4|mp3|ppt|pptx|doc|docx|png|jpg|jpeg';
                $config['max_size']             = 50000;
                $config['encrypt_name']         = TRUE;

                $this->load->library('upload', $config);

                if ($this->upload->do_upload('file')) {
                    $upload_data = $this->upload->data();
                    $data['file_path'] = 'uploads/materials/' . $upload_data['file_name'];
                }
            }
            
            $this->Teacher_model->add_content($data);
            redirect('teacher/class_details/' . $this->input->post('assignment_id'));
        }
    }

    public function update_content() {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $content_id = $this->input->post('content_id');
            $data = [
                'title' => $this->input->post('title'),
                'content_text' => $this->input->post('content_text'),
                'term' => $this->input->post('term')
            ];
            
            // Only update youtube URL if it's a youtube type and provided
            if ($this->input->post('youtube_url')) {
                $data['file_path'] = $this->input->post('youtube_url');
            }

            $this->Teacher_model->update_content($content_id, $data);
            redirect('teacher/class_details/' . $this->input->post('assignment_id'));
        }
    }

    public function delete_content($content_id, $assignment_id) {
        $content = $this->db->get_where('course_content', ['id' => $content_id])->row();
        if ($content) {
            // Delete physical file if it exists and is not a youtube link
            if ($content->file_path && $content->content_type !== 'youtube' && file_exists($content->file_path)) {
                unlink($content->file_path);
            }
            $this->Teacher_model->delete_content($content_id);
        }
        redirect('teacher/class_details/' . $assignment_id);
    }
}
