<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;

/**
 * Enquiry Controller (AJAX)
 *
 * Handles quick enquiry and callback requests from website popup.
 * No login required. All responses are JSON.
 */
class EnquiryController extends Controller
{
    /**
     * Submit a quick enquiry.
     *
     * @return void
     */
    public function store(): void
    {
        $name           = sanitize($_POST['name'] ?? '');
        $contact        = sanitize($_POST['contact'] ?? '');
        $email          = sanitize($_POST['email'] ?? '');
        $courseInterest = sanitize($_POST['course_interest'] ?? '');
        $message        = sanitize($_POST['message'] ?? '');

        if (empty($name) || empty($contact)) {
            $this->json(['success' => false, 'message' => 'Name and contact number are required.'], 422);
            return;
        }

        if (!preg_match('/^[0-9]{10}$/', $contact)) {
            $this->json(['success' => false, 'message' => 'Please enter a valid 10-digit number.'], 422);
            return;
        }

        try {
            $enquiryModel = $this->model('Enquiry');
            $enquiryModel->create([
                'name'           => $name,
                'contact'        => $contact,
                'email'          => $email ?: null,
                'course_interest' => $courseInterest ?: null,
                'message'        => $message ?: null,
                'status'         => 'new',
                'source'         => 'website_popup',
            ]);

            $this->json(['success' => true, 'message' => 'Enquiry submitted successfully! We will contact you soon.']);
        } catch (\Throwable $e) {
            error_log('Enquiry submit error: ' . $e->getMessage());
            $this->json(['success' => false, 'message' => 'Something went wrong. Please try again.'], 500);
        }
    }

    /**
     * Submit a callback request.
     *
     * @return void
     */
    public function callback(): void
    {
        $name           = sanitize($_POST['name'] ?? '');
        $contact        = sanitize($_POST['contact'] ?? '');
        $preferredTime  = sanitize($_POST['preferred_time'] ?? '');
        $courseInterest = sanitize($_POST['course_interest'] ?? '');

        if (empty($name) || empty($contact)) {
            $this->json(['success' => false, 'message' => 'Name and contact number are required.'], 422);
            return;
        }

        if (!preg_match('/^[0-9]{10}$/', $contact)) {
            $this->json(['success' => false, 'message' => 'Please enter a valid 10-digit number.'], 422);
            return;
        }

        try {
            $callbackModel = $this->model('CallbackRequest');
            $callbackModel->create([
                'name'           => $name,
                'contact'        => $contact,
                'preferred_time' => $preferredTime ?: null,
                'course_interest' => $courseInterest ?: null,
                'status'         => 'pending',
                'source'         => 'website_popup',
            ]);

            $this->json(['success' => true, 'message' => 'Callback requested! We will call you soon.']);
        } catch (\Throwable $e) {
            error_log('Callback submit error: ' . $e->getMessage());
            $this->json(['success' => false, 'message' => 'Something went wrong. Please try again.'], 500);
        }
    }
}
