<?php

namespace Tests\Unit\Support;

use App\Support\PromptLandingPageLeadPayload;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Tests\TestCase;

class PromptLandingPageLeadPayloadTest extends TestCase
{
    public function test_it_stores_a_valid_lead_with_name_and_email(): void
    {
        $payload = PromptLandingPageLeadPayload::fromRequest(Request::create('/leads', 'POST', [
            'name' => 'Ada Lovelace',
            'email' => 'ada@example.com',
            'company' => 'Analytical Engines',
        ]));

        $this->assertSame([
            'name' => 'Ada Lovelace',
            'email' => 'ada@example.com',
            'company' => 'Analytical Engines',
        ], $payload);
    }

    public function test_it_rejects_an_invalid_email_value(): void
    {
        try {
            PromptLandingPageLeadPayload::fromRequest(Request::create('/leads', 'POST', [
                'email' => 'not-an-email',
            ]));
            $this->fail('Expected a validation exception.');
        } catch (ValidationException $e) {
            $this->assertSame(['Please enter a valid email address.'], $e->errors()['email']);
        }
    }

    public function test_it_rejects_an_empty_name_when_the_name_field_is_present(): void
    {
        try {
            PromptLandingPageLeadPayload::fromRequest(Request::create('/leads', 'POST', [
                'name' => '   ',
                'email' => 'ada@example.com',
            ]));
            $this->fail('Expected a validation exception.');
        } catch (ValidationException $e) {
            $this->assertSame(['Please fill in the required fields.'], $e->errors()['name']);
        }
    }

    public function test_it_rejects_a_phone_value_without_enough_digits(): void
    {
        try {
            PromptLandingPageLeadPayload::fromRequest(Request::create('/leads', 'POST', [
                'email' => 'ada@example.com',
                'phone' => '123',
            ]));
            $this->fail('Expected a validation exception.');
        } catch (ValidationException $e) {
            $this->assertSame(['Please enter a valid phone number.'], $e->errors()['phone']);
        }
    }

    public function test_it_treats_message_fields_as_optional(): void
    {
        $this->assertTrue(PromptLandingPageLeadPayload::isOptionalLeadField('message'));
        $this->assertTrue(PromptLandingPageLeadPayload::looksLikeEmailField('Email_Address'));
        $this->assertTrue(PromptLandingPageLeadPayload::looksLikeNameField('full-name'));
        $this->assertFalse(PromptLandingPageLeadPayload::looksLikeNameField('company_name'));
    }

    public function test_it_keeps_query_params_out_of_the_form_payload(): void
    {
        $request = Request::create('/leads', 'POST', [
            'email' => 'ada@example.com',
            'page_load_id' => '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
            'query_params' => ['utm_source' => 'facebook'],
        ]);

        $this->assertSame(['email' => 'ada@example.com'], PromptLandingPageLeadPayload::fromRequest($request));
        $this->assertSame('9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d', PromptLandingPageLeadPayload::pageLoadId($request));
    }

    public function test_it_cleans_query_params_from_the_show_request(): void
    {
        $this->assertSame([
            'utm_source' => 'facebook',
            'utm_campaign' => 'spring',
        ], PromptLandingPageLeadPayload::fromQueryArray([
            'utm_source' => 'facebook',
            'utm_campaign' => 'spring',
            'page_load_id' => 'ignored',
            'query_params' => 'ignored',
            '_token' => 'ignored',
        ]));
    }
}
