<?php

// Include the database connection
include 'db_connect.php';

require 'email_functions.php';


// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Handling form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Validate and sanitize inputs
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    // Validate each field individually
    if (empty($name)) {
        echo json_encode(array("message" => "Name field is required", "error" => true));
        exit();
    }

    if (empty($email)) {
        echo json_encode(array("message" => "Email field is required", "error" => true));
        die;
    }

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo json_encode(array("message" => "Email is already taken", "error" => true));
        die;
    }

    if (empty($phone)) {
        echo json_encode(array("message" => "Phone field is required", "error" => true));
        exit();
    }

    if (empty($subject)) {
        echo json_encode(array("message" => "Subject field is required", "error" => true));
        exit();
    }

    if (empty($message)) {
        echo json_encode(array("message" => "Message field is required", "error" => true));
        exit();
    }

    // Sanitize inputs to prevent SQL injection
    $name = mysqli_real_escape_string($conn, $name);
    $email = mysqli_real_escape_string($conn, $email);
    $phone = mysqli_real_escape_string($conn, $phone);
    $subject = mysqli_real_escape_string($conn, $subject);
    $message = mysqli_real_escape_string($conn, $message);

    // Check if email already exists
    $check_sql = "SELECT * FROM questionans WHERE email = '$email'";
    $check_result = $conn->query($check_sql);

    if ($check_result->num_rows > 0) {

        die(json_encode(array("message" => "Email is already taken", "error" => true)));
    }

    // Insert data into database
    $insert_sql = "INSERT INTO questionans (name, email, phone, subject, message, created_at, updated_at)
                   VALUES ('$name', '$email', '$phone', '$subject', '$message', NOW(), NOW())";



    if ($conn->query($insert_sql) === true) {

        try {
            $to = 'divyang@webplanex.us';
            $subject = "Testing";
            $templateFile = '../email/questions.html';

            $replaceValues = array(
                '$name' => $name,
                '$email' => $email,
                '$phone' => $phone,
                '$subject' => $subject,
                '$message' => $message,

            );

            $message1 = generateEmailTemplate($templateFile, $replaceValues);
            sendEmail($to, $subject, $message1);

        } catch (\Throwable $th) {
            // Handle email exception
        }
        echo json_encode(array("message" => "New Questions created successfully", "error" => false));

    } else {
        echo json_encode(array("message" => $conn->error, "error" => true));
    }
}
$conn->close();