<?php
    include "config.php";
    include "../admin/models/Message.php";
    include "../admin/models/Image.php";

    include "../admin/services/MessageService.php";
    include "../admin/services/ImageService.php";

    $message_service = new MessageService($db);
    $image_service = new ImageService($db);

    if (isset($_POST['send'])) {
        
        if ($_POST['captcha_challenge'] == $_SESSION['captcha_text']){
            $message = new Message(
                0,
                $_POST['name'],
                $_POST['email'],
                $_POST['subject'],
                $_POST['message']
            );
            
            $result = $message_service->add($message);
            
            if($result){
                $message = "Thank you for your message.";
                $submessage = "We will contact you as soon as possible.";
            } else {
                $message = "Some error occurred.";
                $submessage = "Please try again or contact via other means.";
            }
        } else {
            $message = "Captcha Error";
            $submessage = "Sorry, your message was not sent.";
        }
    } else {
        redirect("../");
    }
?>

