<?php

namespace App\Controllers;

class ContactController {
    private $pdo;
    private $config;
    
    public function __construct($pdo, $config) {
        $this->pdo = $pdo;
        $this->config = $config;
    }
    
    public function index() {
        $success = '';
        $error = '';
        
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            if (!verify_csrf($_POST['csrf_token'] ?? '')) {
                $error = 'Invalid request.';
            } else {
                $name = trim($_POST['name'] ?? '');
                $email = trim($_POST['email'] ?? '');
                $message = trim($_POST['message'] ?? '');
                
                if (empty($name) || empty($email) || empty($message)) {
                    $error = 'Please fill all fields.';
                } else {
                    $adminEmail = get_setting('academic_email', 'academic@cixconnect.co.za');
                    $body = '<p><strong>From:</strong> ' . e($name) . ' (' . e($email) . ')</p><p>' . nl2br(e($message)) . '</p>';
                    send_email($adminEmail, 'New Contact Form Submission - CIX Orbit Academy', $body);
                    $success = 'Message sent successfully! We will get back to you soon.';
                }
            }
        }
        
        $theme = get_setting('default_theme', 'orbit-dark');
        include __DIR__ . '/../Views/layouts/public.php';
        include __DIR__ . '/../Views/pages/contact.php';
        include __DIR__ . '/../Views/layouts/footer.php';
    }
}
