<?php
session_start();


class LoginController
{
    private $con;

    public function __construct()
    {
        include_once("../../includes/DB/Connection.php");
        $db = new Connection();
        $this->con = $db->connect();
    }

    public function userLogin($request)
    {

        // return $request;
        // Get and trim the email from the request
        $user_email = trim($request['email']);
        // Get the password from the request
        $user_password = $request['password'];

        // Check if both email and password are provided
        if (!empty($user_email) && !empty($user_password)) {
            // Prepare a SQL statement to find the user by email
            $stmt = $this->con->prepare("
                SELECT s.student_id, s.student_first_name, s.student_last_name, s.student_email, s.student_password, 
                       s.campus_id, s.branch_id, s.user_role_id, ur.user_role_name 
                FROM student s 
                INNER JOIN user_role ur ON s.user_role_id = ur.user_role_id
                WHERE s.student_email = ? AND s.student_status = 1
            ");
            $stmt->bind_param("s", $user_email);
            $stmt->execute();
            $result = $stmt->get_result();

            // Check if a user is found
            if ($result->num_rows == 1) {
                $row = $result->fetch_assoc();

                // Verify the provided password with the stored hash
                if (password_verify($user_password, $row['student_password'])) {

// Start session if not already started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

                    // Set session variables upon successful login
                    $_SESSION['log_session'] = [
                        'user_id' => $row['student_id'],
                        'user_first_name' => $row['student_first_name'],
                        'user_last_name' => $row['student_last_name'],
                        'user_email' => $row['student_email'],
                        'campus_id' => $row['campus_id'],
                        'branch_id' => $row['branch_id'],
                        'user_role_id' => $row['user_role_id'],
                        'user_role_name' => $row['user_role_name']
                    ];

                    // Return success response
                    $data = [
                        'user_role_id' => $row['user_role_id'],
                        'user_role_name' => $row['user_role_name'],
                        'message' => 'Login Success!'
                    ];
                    echo json_encode(['status' => 1, 'data' => $data]);
                    exit();
                }
                 else {
                    // Incorrect password
                    echo json_encode(['status' => 0, 'message' => 'Incorrect Password!']);
                    exit();
                }
            }
             else {
                // No user found with the provided email or account inactive
                echo json_encode(['status' => 0, 'message' => 'Incorrect Email Address or Account Inactive!']);
                exit();
            }
        } 
        else {
            // Email or password not provided
            echo json_encode(['status' => 0, 'message' => 'Both Email and Password are required!']);
            exit();
        }
    }
}

// Check if the login form is submitted
if (isset($_POST['USER_LOGIN'])) {
    $lc = new LoginController();
    echo json_encode($lc->userLogin($_POST));
exit();
    
}