<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Include PHPMailer
require 'includes/PHPMailer/src/Exception.php';
require 'includes/PHPMailer/src/PHPMailer.php';
require 'includes/PHPMailer/src/SMTP.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Sanitize Input
    $name = htmlspecialchars($_POST['client_name']);
    $mobile = htmlspecialchars($_POST['client_mobile']);
    $email = filter_var($_POST['client_email'], FILTER_SANITIZE_EMAIL);
    $state = htmlspecialchars($_POST['client_state']);
    $position = htmlspecialchars($_POST['client_position']);
    $experience = htmlspecialchars($_POST['client_experience']);
    $country = htmlspecialchars($_POST['client_country']);

    // Validate Email
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        exit("error: Invalid email format");
    }

    // Receiver Email
    $to = "info@asadenterprises.in";

    // Email Body
    $message = "New Job Application Received\n";
    $message .= "Name: $name\nMobile: $mobile\nEmail: $email\nState: $state\nPosition: $position\nExperience: $experience\nWorked in Country: $country\n";

    // PHPMailer Setup
    $mail = new PHPMailer(true);

    try {
        // SMTP Configuration for cPanel Email
        $mail->isSMTP();
        $mail->Host = 'mail.asadenterprises.in'; 
        $mail->SMTPAuth = true;
        $mail->Username = 'team@asadenterprises.in'; // ✅ Hidden Email
        $mail->Password = 'Team@12345678@'; // ❌ NEVER Share Password Publicly
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port = 587;
        $mail->Timeout = 10;

        // ✅ SSL Verification Disable (Speed Up)
        $mail->SMTPOptions = [
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            ]
        ];

        // ✅ Email Headers (Only "Asad Enterprises" Will Show)
        $mail->setFrom('no-reply@asadenterprises.in', 'Asad Enterprises'); // ✅ Hides sender email
        $mail->addAddress($to);
        $mail->addReplyTo($email, $name); // ✅ Reply-To set to user input
        $mail->Sender = 'bounce@asadenterprises.in'; // ✅ Sender email hidden from recipient

        // ✅ Resume Attachment Handling
        if (!empty($_FILES['client_resume']['tmp_name']) && $_FILES['client_resume']['error'] == 0) {
            $allowed_types = ['pdf', 'doc', 'docx'];
            $file_tmp = $_FILES['client_resume']['tmp_name'];
            $file_name = $_FILES['client_resume']['name'];
            $file_size = $_FILES['client_resume']['size'];
            $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

            if (in_array($file_ext, $allowed_types) && $file_size <= 2 * 1024 * 1024) {
                $mail->addAttachment($file_tmp, $file_name);
            } else {
                exit("error: Invalid file type or size exceeded");
            }
        }

        // Send Plain Text Email
        $mail->isHTML(false);
        $mail->Subject = "New Job Application - $position";
        $mail->Body = $message;

        // ✅ Send Mail & Redirect to Index
        if ($mail->send()) {
            header("Location: index.php"); // ✅ Success pe Redirect
            exit();
        } else {
            exit("error: " . $mail->ErrorInfo);
        }

    } catch (Exception $e) {
        exit("error: " . $mail->ErrorInfo);
    }
}
?>
