<?php
namespace App\Controllers;

use App\Core\Controller;

class Home extends Controller
{
    public function index()
    {
        $this->view('inc/header.php');
        $this->view('public/home.php');
        $this->view('inc/footer.php');
    }

    public function contact()
    {
        $messageModel = $this->model('Message');
        $sent = false;
        $error = '';

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

            if ($name && $email && $subject && $message) {
                $saved = $messageModel->save(["name" => $name, "email" => $email, "subject" => $subject, "message" => $message]);
                if ($saved) {
                    $sent = true;
                } else {
                    $error = 'Could not save your message. Please try again.';
                }
            } else {
                $error = 'Please complete all fields.';
            }
        }

        $this->view('inc/header.php');
        $this->view('public/contact.php', ['sent' => $sent, 'error' => $error]);
        $this->view('inc/footer.php');
    }
}
