<?php

require __DIR__ . '/../vendor/autoload.php';

use Dotenv\Dotenv;

$dotenv = Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->load();

require_once __DIR__ . '/../config/mail.php';

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['message' => 'Method not allowed']);
    exit;
}

$name = trim($_POST['fname'] ?? '');
$email = trim($_POST['femail'] ?? '');
$mobile = trim($_POST['fphone'] ?? '');
$fservice = trim($_POST['fservice'] ?? '');

if (!$name || !$email || !$fservice) {
    http_response_code(422);
    echo json_encode(['message' => 'Fill required fields please', 'data' => $_POST]);
    exit;
}

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    http_response_code(422);
    echo json_encode(['message' => 'Invalid email']);
    exit;
}
// print_r($lines); return;

$sent = sendMail($email, $name, $mobile, $fservice);

echo json_encode([
    'success' => $sent
]);