<?php
namespace App\Controllers;

use Core\View;
use Core\Request;
use Core\Response;
use Core\Validator;
use Core\Helpers\EmailHelper;

class HomeController
{
    public function index()
    {
        View::make('home')
            ->setLayout('layout')
            ->with([
                'title' => 'Emergency Response Coordination System',
                'meta_description' => 'Fast and reliable emergency reporting and dispatch system.'
            ])
            ->render();
    }

    public function aboutPage()
    {
        View::make('about')
            ->setLayout('layout')
            ->with([
                'title' => 'About Us - Emergency Response'
            ])
            ->render();
    }

    public function contactPage()
    {
        View::make('contact')
            ->setLayout('layout')
            ->with([
                'title' => 'Contact Us - Support & Inquiries'
            ])
            ->render();
    }

    public function submitContactForm()
    {
        $validator = Validator::validate($_POST)
            ->empty(['name', 'email', 'message'])
            ->email(['email']);

        if (!$validator->passes()) {
            return Response::json(['status' => 'error', 'errors' => $validator->errors()], Response::BAD_REQUEST);
        }

        $data = Request::all();

        // Queue email notification to the Admin
        $adminBody = View::renderForEmail('contact_notification', [
            'name' => $data['name'],
            'email' => $data['email'],
            'message' => $data['message']
        ]);

        EmailHelper::queueEmail(
            'admin@emergency.local', 
            'New Contact Inquiry: ' . $data['name'], 
            $adminBody
        );

        return Response::json(['status' => 'success', 'message' => 'Your message has been sent successfully.']);
    }
}