<?php
declare(strict_types=1);

namespace App\Controller;

use Cake\Core\Configure;
use Cake\Http\Client;
use Cake\Log\Log;

class EnquiriesController extends AppController
{
    public function initialize(): void
    {
        parent::initialize();

        
        $this->Authentication->allowUnauthenticated(['add']);
    }

    // ================= ADD ENQUIRY =================
    public function add()
    {
        $this->request->allowMethod(['post']);

        $enquiry = $this->Enquiries->newEmptyEntity();

        $data = $this->request->getData();
        $turnstileToken = $data['cf-turnstile-response'] ?? null;
        unset($data['cf-turnstile-response']);

        if ($turnstileToken === null || $turnstileToken === '') {
            $this->Flash->error('Please complete the verification before sending your message.');

            return $this->redirect($this->referer() ?? '/');
        }

        $secret = Configure::read('Captcha.turnstile.secretKey');
        if ($secret === null || $secret === '') {
            Log::warning('Turnstile secret key is not configured.');
            $this->Flash->error('Verification is temporarily unavailable. Please try again later.');

            return $this->redirect($this->referer() ?? '/');
        }

        $http = new Client();
        $verify = $http->post(
            'https://challenges.cloudflare.com/turnstile/v0/siteverify',
            [
                'secret' => $secret,
                'response' => $turnstileToken,
                'remoteip' => $this->request->clientIp(),
            ]
        );

        if (!$verify->isOk()) {
            $this->Flash->error('Verification failed. Please try again.');

            return $this->redirect($this->referer() ?? '/');
        }

        $verifyJson = $verify->getJson();
        if (empty($verifyJson['success'])) {
            $this->Flash->error('Verification failed. Please complete the check again.');

            return $this->redirect($this->referer() ?? '/');
        }

        Log::debug('ENQUIRY DATA: ' . json_encode($data));

        // Default enquiry status
        $data['status'] = 'pending';

        
        $user = $this->request->getAttribute('identity');
        if ($user) {
            $data['user_id'] = $user->id;
        }

        $enquiry = $this->Enquiries->patchEntity($enquiry, $data);

        if ($enquiry->getErrors()) {
            Log::error('ENQUIRY VALIDATION ERROR: ' . json_encode($enquiry->getErrors()));
        }

        if ($this->Enquiries->save($enquiry)) {
            Log::debug('ENQUIRY SAVED ID: ' . $enquiry->id);

            $this->Flash->success('Your message has been sent successfully');

        } else {
            Log::error('ENQUIRY SAVE FAILED');

            $this->Flash->error('Failed to send message. Please try again');
        }

        return $this->redirect('/');
    }

    // ================= ADMIN LIST =================
    public function index()
    {
        $this->checkAdmin();

        $enquiries = $this->paginate(
            $this->Enquiries->find()->order(['created' => 'DESC'])
        );

        $this->set(compact('enquiries'));
    }

    // ================= UPDATE STATUS =================
    public function updateStatus($id, $status)
    {
        $this->checkAdmin();

        $enquiry = $this->Enquiries->get($id);

        $enquiry->status = $status;

        if ($this->Enquiries->save($enquiry)) {
            $this->Flash->success('Status updated');
        } else {
            $this->Flash->error('Update failed');
        }

        return $this->redirect(['action' => 'index']);
    }

    // ================= DELETE =================
    public function delete($id)
    {
        $this->checkAdmin();

        $this->request->allowMethod(['post']);

        $enquiry = $this->Enquiries->get($id);

        if ($this->Enquiries->delete($enquiry)) {
            $this->Flash->success('Deleted');
        } else {
            $this->Flash->error('Delete failed');
        }

        return $this->redirect(['action' => 'index']);
    }

    // ================= ADMIN CHECK =================
    private function checkAdmin()
    {
        $user = $this->request->getAttribute('identity');

        if (!$user || $user->role !== 'admin') {
            throw new \Cake\Http\Exception\ForbiddenException('Access denied');
        }
    }
    public function view($id)
{
    $this->checkAdmin();

    $enquiry = $this->Enquiries->get($id);
    $this->set(compact('enquiry'));
}
    public function markReplied($id)
{
    $this->checkAdmin();

    $enquiry = $this->Enquiries->get($id);
    $enquiry->status = 'replied';

    if ($this->Enquiries->save($enquiry)) {
        $this->Flash->success('Marked as replied');
    } else {
        $this->Flash->error('Failed');
    }

    return $this->redirect(['action' => 'index']);
}
}