<?php

require_once APP_ROOT . '/models/ContactMessage.php';
require_once APP_ROOT . '/models/Setting.php';
require_once APP_ROOT . '/helpers/Mailer.php';

class PageController
{
    public function support(): void
    {
        require APP_ROOT . '/views/pages/support.php';
    }

    public function contact(): void
    {
        require APP_ROOT . '/views/pages/contact.php';
    }

    public function terms(): void
    {
        require APP_ROOT . '/views/pages/terms.php';
    }

    public function privacy(): void
    {
        require APP_ROOT . '/views/pages/privacy.php';
    }

    public function notFound(): void
    {
        http_response_code(404);
        require APP_ROOT . '/views/pages/404.php';
    }

    public function sendContact(): void
    {
        if (!Session::verifyCsrfToken($_POST['csrf_token'] ?? '')) {
            Session::flash('error', 'Invalid request.');
            header('Location: ' . APP_URL . '/contact');
            exit;
        }

        $recaptchaToken = $_POST['recaptcha_token'] ?? '';
        if (!is_string($recaptchaToken) || !Recaptcha::verifyToken($recaptchaToken)) {
            Session::flash('error', Lang::get('recaptcha_error'));
            header('Location: ' . APP_URL . '/contact');
            exit;
        }

        $firstName = trim($_POST['first_name'] ?? '');
        $lastName = trim($_POST['last_name'] ?? '');
        $email = trim($_POST['email'] ?? '');
        $subject = trim($_POST['subject'] ?? '');
        $message = trim($_POST['message'] ?? '');

        $validator = new Validator();
        $validator->required('first_name', $firstName, Lang::get('first_name'));
        $validator->required('last_name', $lastName, Lang::get('last_name'));
        $validator->required('email', $email, Lang::get('email'));
        $validator->required('subject', $subject, Lang::get('subject'));
        $validator->required('message', $message, Lang::get('message'));

        if ($validator->fails()) {
            Session::flash('error', $validator->firstError());
            header('Location: ' . APP_URL . '/contact');
            exit;
        }

        $contactModel = new ContactMessage();
        $contactModel->create([
            'first_name' => $firstName,
            'last_name'  => $lastName,
            'email'      => $email,
            'subject'    => $subject,
            'message'    => $message,
        ]);

        $settingModel = new Setting();
        $adminEmail = $settingModel->get('admin_email');

        if (!empty($adminEmail)) {
            $htmlBody = "<h2>" . htmlspecialchars($subject) . "</h2>"
                . "<p><strong>" . Lang::get('first_name') . ":</strong> " . htmlspecialchars($firstName) . "</p>"
                . "<p><strong>" . Lang::get('last_name') . ":</strong> " . htmlspecialchars($lastName) . "</p>"
                . "<p><strong>" . Lang::get('email') . ":</strong> " . htmlspecialchars($email) . "</p>"
                . "<p><strong>" . Lang::get('message') . ":</strong></p>"
                . "<p>" . nl2br(htmlspecialchars($message)) . "</p>";

            $emailSent = Mailer::send($adminEmail, Lang::get('app_name') . ' - ' . $subject, $htmlBody);
            if (!$emailSent) {
                error_log('Contact form: Failed to send email notification to ' . $adminEmail);
            }
        }

        Session::flash('success', Lang::get('contact_sent'));
        header('Location: ' . APP_URL . '/contact');
        exit;
    }
}
