<?php
declare(strict_types=1);

namespace App\Test\TestCase\Controller;

use Cake\Core\Configure;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;

class ContactMessagesControllerTest extends TestCase
{
    use IntegrationTestTrait;

    protected function setUp(): void
    {
        parent::setUp();
        Configure::write('Turnstile.skipVerification', true);
        $this->setUnlockedFields(['cf-turnstile-response', 'website']);
    }

    protected function tearDown(): void
    {
        Configure::delete('Turnstile.skipVerification');
        parent::tearDown();
    }

    public function testAddRendersForm(): void
    {
        $this->get('/contact');

        $this->assertResponseOk();
        $this->assertResponseContains('Send an enquiry');
        $this->assertResponseContains('cf-turnstile');
    }

    public function testAddCreatesMessageOnValidSubmit(): void
    {
        $this->enableCsrfToken();
        $this->enableSecurityToken();

        $messages = TableRegistry::getTableLocator()->get('ContactMessages');
        $beforeCount = $messages->find()->count();

        $this->post('/contact', [
            'full_name' => 'Jordan Weaver',
            'email' => 'jordan.weaver@example.com',
            'phone' => '0400111222',
            'interest_area' => 'pottery',
            'subject' => 'First class question',
            'message_body' => 'I am interested in a beginner pottery pathway and want to know how the first session works.',
            'consent_to_contact' => '1',
            'cf-turnstile-response' => 'test-token',
            'website' => '',
        ]);

        $this->assertRedirectContains('/contact/success');
        $this->assertSame($beforeCount + 1, $messages->find()->count());

        $savedMessage = $messages->find()->where(['email' => 'jordan.weaver@example.com'])->firstOrFail();
        $this->assertSame('pottery', $savedMessage->interest_area);
        $this->assertSame('new', $savedMessage->status);
    }

    public function testAddRejectsFilledHoneypot(): void
    {
        $this->enableCsrfToken();
        $this->enableSecurityToken();

        $messages = TableRegistry::getTableLocator()->get('ContactMessages');
        $beforeCount = $messages->find()->count();

        $this->post('/contact', [
            'full_name' => 'Bot Visitor',
            'email' => 'bot.visitor@example.com',
            'interest_area' => 'both',
            'message_body' => 'This message looks valid but should be blocked because the honeypot field is filled in.',
            'consent_to_contact' => '1',
            'cf-turnstile-response' => 'test-token',
            'website' => 'https://spam.example.com',
        ]);

        $this->assertResponseOk();
        $this->assertResponseContains('Please review your submission and try again.');
        $this->assertSame($beforeCount, $messages->find()->count());
    }

    public function testAddShowsValidationErrorsForInvalidData(): void
    {
        $this->enableCsrfToken();
        $this->enableSecurityToken();

        $this->post('/contact', [
            'full_name' => '',
            'email' => 'invalid',
            'interest_area' => '',
            'message_body' => 'Too short',
            'consent_to_contact' => '0',
            'cf-turnstile-response' => 'test-token',
            'website' => '',
        ]);

        $this->assertResponseOk();
        $this->assertResponseContains('Your enquiry could not be sent.');
        $this->assertResponseContains('Please share a little more detail so the team can follow up clearly.');
        $this->assertResponseContains('You must consent before submitting.');
    }
}
