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

class Auth extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('User_model');
        $this->load->library('session');
        $this->load->helper(['url', 'form']);
    }

    // Show login form
    public function login() {
        if ($this->session->userdata('user_id')) {
            $this->_redirect_role($this->session->userdata('role'));
            return;
        }
        $this->load->view('auth/login');
    }

    // Process login submission
    public function do_login() {
        $username = $this->input->post('username', true);
        $password = $this->input->post('password', true);
        $user = $this->User_model->authenticate($username, $password);
        if ($user) {
            $this->session->set_userdata([
                'user_id'   => $user->id,
                'username'  => $user->username,
                'role'      => $user->role,
                'logged_in' => true
            ]);
            $this->_redirect_role($user->role);
        } else {
            $data['error'] = 'Invalid username or password';
            $this->load->view('auth/login', $data);
        }
    }

    // Show registration form
    public function register() {
        if ($this->session->userdata('user_id')) {
            $this->_redirect_role($this->session->userdata('role'));
            return;
        }
        $this->load->view('auth/register');
    }

    // Process registration
    public function do_register() {
        $data = [
            'full_name'   => $this->input->post('full_name', true),
            'username'    => $this->input->post('username', true),
            'email'       => $this->input->post('email', true),
            'password'    => $this->input->post('password', true),
            'phone'       => $this->input->post('phone', true),
            'class_level' => $this->input->post('class_level', true),
            'gender'      => $this->input->post('gender', true)
        ];

        // Basic validation
        if (empty($data['full_name']) || empty($data['username']) || empty($data['email']) || empty($data['password']) || empty($data['phone']) || empty($data['class_level']) || empty($data['gender'])) {
            $view_data['error'] = 'All fields are required.';
            $this->load->view('auth/register', $view_data);
            return;
        }

        if ($this->User_model->register_student($data)) {
            $this->session->set_flashdata('success', 'Registration successful! You can now log in.');
            redirect('auth/login');
        } else {
            $view_data['error'] = 'Registration failed. Username, email, or phone number might already be taken.';
            $this->load->view('auth/register', $view_data);
        }
    }

    // Logout
    public function logout() {
        $this->session->sess_destroy();
        redirect('auth/login');
    }

    private function _redirect_role($role) {
        switch ($role) {
            case 'admin':
                redirect('admin/dashboard');
                break;
            case 'teacher':
                redirect('teacher/dashboard');
                break;
            case 'student':
                redirect('student/dashboard');
                break;
            case 'accountant':
                redirect('accountant/dashboard');
                break;
            case 'librarian':
                redirect('librarian/dashboard');
                break;
            case 'super_admin':
                redirect('admin/dashboard');
                break;
            default:
                redirect('auth/login');
        }
    }
}
?>
