<?php
header("Access-Control-Allow-Origin: *"); // Allow access from any origin

// Import PHPMailer classes
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
require 'classes/vendor/autoload.php';

// Function to send email
function sendEmail($fullname, $email, $gender, $phone, $address, $academicLevel, $expertise, $skills, $cvData, $cvName, $coverLetterData, $coverLetterName) {
    $mail = new PHPMailer(true);
    try {
        // Server settings
        $mail->isSMTP();
        $mail->Host       = 'burasolutions.com'; 
        $mail->SMTPAuth   = true;
        $mail->Username   = 'api.server@burasolutions.com'; 
        $mail->Password   = '}7K85RiM#hJU';
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port       = 587;

        // Recipients
        $mail->setFrom('api.server@burasolutions.com', 'Burasolutions');
        // $mail->addAddress("aminigbopaul@gmail.com");
        $mail->addAddress("mannehills@gmail.com");
        $mail->addAddress("buradum@burasolutions.com");
        $mail->addReplyTo('api.server@burasolutions.com', 'Hello');

        // Message content
        $msg = '<!DOCTYPE html>
        <html lang="en">
        <head><meta charset="UTF-8"><title>Application</title></head>
        <body>
        <p>Fullname: '.$fullname.'</p>
        <p>Email: '.$email.'</p>
        <p>Gender: '.$gender.'</p>
        <p>Phone: '.$phone.'</p>
        <p>Address: '.$address.'</p>
        <p>Highest Academic Level: '.$academicLevel.'</p>
        <p>Level of Expertise: '.$expertise.'</p>
        <p>Relevant skills: '.$skills.'</p>
        </body>
        </html>';

        $mail->isHTML(true);
        $mail->Subject = 'Career Application';
        $mail->Body    = $msg;

        // Attach CV and Cover Letter
        if ($cvData && $cvName) {
            $mail->addStringAttachment($cvData, $cvName);
        }
        if ($coverLetterData && $coverLetterName) {
            $mail->addStringAttachment($coverLetterData, $coverLetterName);
        }

        $mail->send();
        return "Message has been sent:::{$email}";
    } catch (Exception $e) {
        return "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
}

// Check for POST request and required parameters
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $fullname = $_POST['FULLNAME'];
    $email = $_POST['EMAIL'];
    $gender = $_POST['GENDER'];
    $phone = $_POST['PHONE'];
    $address = $_POST['ADDRESS'];
    $academicLevel = $_POST['ACADEMICLEVEL'];
    $expertise = $_POST['EXPERTIES'];
    $skills = $_POST['SKILLS'];

    // Handle file uploads
    $cvData = $cvName = null;
    $coverLetterData = $coverLetterName = null;

    if (isset($_FILES['CV']) && $_FILES['CV']['error'] == UPLOAD_ERR_OK) {
        $cvData = file_get_contents($_FILES['CV']['tmp_name']);
        $cvName = $_FILES['CV']['name'];
    }

    if (isset($_FILES['COVER_LETTER']) && $_FILES['COVER_LETTER']['error'] == UPLOAD_ERR_OK) {
        $coverLetterData = file_get_contents($_FILES['COVER_LETTER']['tmp_name']);
        $coverLetterName = $_FILES['COVER_LETTER']['name'];
    }

    // Send email
    $response = sendEmail($fullname, $email, $gender, $phone, $address, $academicLevel, $expertise, $skills, $cvData, $cvName, $coverLetterData, $coverLetterName);
    echo json_encode(['status' => 'success', 'message' => $response]);
} else {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request method']);
}
?>
