<?php

namespace App\Controllers;

use App\Models\RegistrationModel;
use App\Models\CourseModel;


class Student extends BaseController
{
    public function login()
    {
        $session = session();

        // Redirect if already logged in
        if ($session->get('student_logged_in')) {
            return redirect()->to(site_url('student/dashboard'));
        }

        // Handle login submission
        if (strtoupper($this->request->getMethod()) === 'POST') {
            $credential = $this->request->getPost('credential'); // Email or Phone
            $password   = $this->request->getPost('password');

            $model = new RegistrationModel();
            // Look up student by email or phone
            $student = $model->where('email', $credential)
                             ->orWhere('phone', $credential)
                             ->first();

            if ($student) {
                // Ensure the student has paid and enrollment is completed
                if ($student['status'] !== 'completed') {
                    $data['error'] = 'Your enrollment is pending. Please complete the payment.';
                    return view('student_login', $data);
                }

                // Verify password if set
                if (!empty($student['password'])) {
                    if (empty($password) || !password_verify($password, $student['password'])) {
                        $data['error'] = 'Invalid password entered.';
                        return view('student_login', $data);
                    }
                }

                $session->set([
                    'student_logged_in' => true,
                    'student_id'        => $student['id'],
                    'student_name'      => $student['name'],
                    'student_email'     => $student['email'],
                    'student_course'    => $student['course'],
                ]);
                return redirect()->to(site_url('student/dashboard'));
            } else {
                $data['error'] = 'No enrolled student found with this email or phone number.';
                return view('student_login', $data);
            }
        }

        return view('student_login');
    }

    public function dashboard()
    {
        $session = session();

        if (!$session->get('student_logged_in')) {
            return redirect()->to(site_url('student/login'));
        }

        $studentCourse = $session->get('student_course');

        $courseModel = new CourseModel();

        $data['student_name']   = $session->get('student_name');
        $data['student_email']  = $session->get('student_email');
        $data['student_course'] = $studentCourse;
        $data['course_type']    = $courseModel->getCourseType($studentCourse);
        $data['syllabus']       = $courseModel->getGroupedModules($studentCourse);
        $data['flat_lessons']   = $courseModel->getLessonsByCourseCode($studentCourse);

        return view('student_dashboard', $data);
    }


    public function logout()
    {
        $session = session();
        $session->remove([
            'student_logged_in',
            'student_id',
            'student_name',
            'student_email',
            'student_course'
        ]);
        return redirect()->to(site_url('student/login'));
    }
}
