<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Load .env variables
require_once __DIR__ . '/load_env.php';

// Load manual PHPMailer from /lib
require_once __DIR__ . '/../lib/PHPMailer/src/PHPMailer.php';
require_once __DIR__ . '/../lib/PHPMailer/src/SMTP.php';
require_once __DIR__ . '/../lib/PHPMailer/src/Exception.php';

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

$alert = "";

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name    = trim($_POST['name']);
    $email   = trim($_POST['email']);
    $subject = trim($_POST['subject']);
    $message = trim($_POST['message']);

    try {
        // Send to Support
        $mail = new PHPMailer(true);
        $mail->isSMTP();
        $mail->Host       = $_ENV['BREVO_SMTP_HOST'];
        $mail->SMTPAuth   = true;
        $mail->Username   = $_ENV['BREVO_SMTP_USER'];
        $mail->Password   = $_ENV['BREVO_SMTP_PASS'];
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port       = (int)$_ENV['BREVO_SMTP_PORT'];

        $mail->setFrom($_ENV['BREVO_FROM_EMAIL'], $_ENV['BREVO_FROM_NAME']);
        $mail->addAddress($_ENV['SUPPORT_EMAIL'], 'Support Team');

        $mail->isHTML(true);
        $mail->Subject = "Support Message: $subject";
        $mail->Body = "
        New support request submitted via <a href='https://custora.digivortlimited.com' target='_blank'>Custora CRM</a> Contact Page:<br><br>
        
        <strong>Name:</strong> $name<br>
        <strong>Email:</strong> $email<br>
        <strong>Subject:</strong> $subject<br><br>
        
        <strong>Message:</strong><br>
        $message<br><br>
        
        Please respond to the user directly.<br><br>
        
        Regards,<br>
        <a href='https://custora.digivortlimited.com' target='_blank'>Custora CRM Contact Portal</a>
        ";
        $mail->AltBody = strip_tags($mail->Body);
        $mail->send();

        // Auto-reply to Guest
        $reply = new PHPMailer(true);
        $reply->isSMTP();
        $reply->Host       = $_ENV['BREVO_SMTP_HOST'];
        $reply->SMTPAuth   = true;
        $reply->Username   = $_ENV['BREVO_SMTP_USER'];
        $reply->Password   = $_ENV['BREVO_SMTP_PASS'];
        $reply->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $reply->Port       = (int)$_ENV['BREVO_SMTP_PORT'];

        $reply->setFrom($_ENV['BREVO_FROM_EMAIL'], $_ENV['BREVO_FROM_NAME']);
        $reply->addAddress($email, $name);

        $reply->isHTML(true);
        $reply->Subject = "We received your message - Custora CRM Support";
        $reply->Body = "
        Hello $name,<br><br>
        
        Thank you for contacting <a href='https://custora.digivortlimited.com' target='_blank'>Custora CRM</a> Support.<br><br>
        
        We have received your message with the subject <strong>'$subject'</strong>, and one of our team members will get back to you shortly.<br><br>
        
        Here is a copy of your message:<br>
        ---------------------------------------------------------<br>
        $message<br>
        ---------------------------------------------------------<br><br>
        
        Best Regards,<br>
        <a href='https://custora.digivortlimited.com' target='_blank'>Custora CRM Support</a><br>
        Digital Vortex Technologies Limited
        ";
        $reply->AltBody = strip_tags($reply->Body);
        $reply->send();

        $alert = '<div class="alert success">Your message has been sent successfully! Check your email for a confirmation.</div>';

    } catch (Exception $e) {
        $alert = '<div class="alert error">Failed to send message. Error: '. $e->getMessage() .'</div>';
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact System Admin</title>
<link rel="icon" href="../public/assets/img/custora_logo.png" type="image/png">
<style>
    /* Reset some defaults */
    * { box-sizing: border-box; margin:0; padding:0; }

    body { font-family: Arial, sans-serif; background: #f4f7fa; }
    header { background: #005f8d; color: #fff; padding: 15px 20px; text-align: center; }
    header h2 { margin: 0; font-size: 1.8rem; }

    .container { 
        max-width: 600px; 
        margin: 40px auto; 
        padding: 25px; 
        background: #fff; 
        border-radius: 12px; 
        box-shadow:0 4px 15px rgba(0,0,0,0.1);
        width: 90%;
    }

    h3 { text-align:center;color:#005f8d;margin-bottom:20px;font-size:1.4rem; }
    .alert { padding:12px;border-radius:8px;margin-bottom:15px;font-size:14px;}
    .success { background:#e6ffed; border:1px solid #27ae60; color:#2e7d32;}
    .error { background:#ffeaea; border:1px solid #e74c3c; color:#c0392b;}

    form { display: flex; flex-direction: column; gap: 12px; }
    label { font-weight:bold; color:#34495e; }
    input, textarea, button { width: 100%; padding:12px; border-radius:8px; border:1px solid #ccc; font-size:14px; }
    input:focus, textarea:focus { outline:none; border-color:#2980b9; box-shadow:0 0 4px rgba(41,128,185,0.5); }
    button { background:#2980b9; color:#fff; font-weight:bold; border:none; cursor:pointer; transition: 0.3s; }
    button:hover { background:#1f6391; }
    a { color:#005f8d; text-decoration:none; text-align:center; margin-top:10px; display:inline-block;}
    a:hover { color:orange; text-decoration:underline; }

    footer { text-align:center; padding:15px; margin-top:40px; background:#f0f0f0; color:#555; font-size:14px; }

    /* Responsive adjustments */
    @media (max-width: 768px) {
        .container { padding: 20px; margin: 30px auto; }
        header h2 { font-size: 1.5rem; }
        h3 { font-size: 1.2rem; }
        input, textarea, button { font-size: 13px; padding:10px; }
    }

    @media (max-width: 480px) {
        .container { padding: 15px; margin: 20px auto; }
        header h2 { font-size: 1.3rem; }
        h3 { font-size: 1rem; }
        input, textarea, button { font-size: 12px; padding:8px; }
    }
</style>
</head>
<body>
<header>
    <h2>Custora CRM</h2>
</header>

<div class="container">
    <h3>Contact the System Administrator</h3>
    <?= $alert ?>

    <form method="POST">
        <label>Your Name:</label>
        <input type="text" name="name" required>

        <label>Your Email:</label>
        <input type="email" name="email" required>

        <label>Subject:</label>
        <input type="text" name="subject" required>

        <label>Message:</label>
        <textarea name="message" rows="5" required></textarea>

        <button type="submit">Send Message</button>
        <a href="../">Back to Dashboard</a>
    </form>
</div>

<footer>
    &copy; 2025 Custora CRM - Digital Vortex Technologies Limited. All rights reserved.
</footer>
</body>
</html>