<?php
session_start();

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Load PHPMailer manually (no Composer)
require_once __DIR__ . '/PHPMailer/src/Exception.php';
require_once __DIR__ . '/PHPMailer/src/PHPMailer.php';
require_once __DIR__ . '/PHPMailer/src/SMTP.php';


/*
|--------------------------------------------------------------------------
| Validate Request
|--------------------------------------------------------------------------
*/
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    header('Location: contact.php');
    exit();
}

/*
|--------------------------------------------------------------------------
| Sanitize Input
|--------------------------------------------------------------------------
*/
function clean_input($data)
{
    return htmlspecialchars(trim($data), ENT_QUOTES, 'UTF-8');
}

$name    = clean_input($_POST['name'] ?? '');
$email   = clean_input($_POST['email'] ?? '');
$phone   = clean_input($_POST['phone'] ?? '');
$subject = clean_input($_POST['subject'] ?? '');
$message = clean_input($_POST['message'] ?? '');

/*
|--------------------------------------------------------------------------
| Validation
|--------------------------------------------------------------------------
*/
$errors = [];

if (empty($name)) {
    $errors[] = "Full Name is required.";
}

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $errors[] = "Valid Email Address is required.";
}

if (empty($subject)) {
    $errors[] = "Subject is required.";
}

if (empty($message)) {
    $errors[] = "Message is required.";
}

if (!empty($errors)) {

    $_SESSION['contact_status'] = implode('<br>', $errors);
    $_SESSION['contact_type'] = 'danger';

    header('Location: contact.php');
    exit();
}

try {

    $mail = new PHPMailer(true);
       // Debug Mode
    // $mail->SMTPDebug = 3;
    // $mail->Debugoutput = 'html';

    /*
    |--------------------------------------------------------------------------
    | SMTP Configuration
    |--------------------------------------------------------------------------
    */

    $mail->isSMTP();

    $mail->SMTPOptions = [
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        ]
    ];

    $config = require_once __DIR__ . '/includes/mail.php';

    $mail->Host       = $config['host'];
    $mail->SMTPAuth   = true;
    $mail->Username   = $config['username'];
    $mail->Password   = $config['password'];
    $mail->Port       = $config['port'];
    $mail->SMTPSecure = $config['encryption'];

    $mail->CharSet = 'UTF-8';

    /*
    |--------------------------------------------------------------------------
    | Sender & Recipient
    |--------------------------------------------------------------------------
    */

    $mail->setFrom(
        // 'info@ultraprocesssolutions.com',
        'info@rejigprocesstechnology.com.ng',
        'UltraProcess Solutions'
    );

    $mail->addAddress(
        //'info@ultraprocesssolutions.com',
         'info@rejigprocesstechnology.com.ng',
        'UltraProcess Solutions'
    );

    $mail->addReplyTo($email, $name);

    /*
    |--------------------------------------------------------------------------
    | Email Content
    |--------------------------------------------------------------------------
    */

    $mail->isHTML(true);

    $mail->Subject = 'Website Contact Form - ' . $subject;

    $mail->Body = "
        <h2>New Website Enquiry</h2>

        <table cellpadding='8' cellspacing='0' border='1' width='100%'>
            <tr>
                <td><strong>Name</strong></td>
                <td>{$name}</td>
            </tr>

            <tr>
                <td><strong>Email</strong></td>
                <td>{$email}</td>
            </tr>

            <tr>
                <td><strong>Phone</strong></td>
                <td>{$phone}</td>
            </tr>

            <tr>
                <td><strong>Subject</strong></td>
                <td>{$subject}</td>
            </tr>

            <tr>
                <td><strong>Message</strong></td>
                <td>" . nl2br($message) . "</td>
            </tr>
        </table>

        <br>

        <p>
            Sent from UltraProcess Solutions Website Contact Form
        </p>
    ";

    $mail->AltBody =
        "Name: {$name}\n" .
        "Email: {$email}\n" .
        "Phone: {$phone}\n" .
        "Subject: {$subject}\n\n" .
        "Message:\n{$message}";

    $mail->send();

    $_SESSION['contact_status'] =
        'Thank you! Your message has been sent successfully.';

    $_SESSION['contact_type'] = 'success';

} catch (Exception $e) {

    $_SESSION['contact_status'] =
        'Message could not be sent. Mailer Error: ' .
        $mail->ErrorInfo;

    $_SESSION['contact_type'] = 'danger';
}

header('Location: contact.php');
exit();