<?php

namespace Tests\Feature;

use App\Mail\EnquiryReceived;
use App\Models\Enquiry;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;

class EnquiryTest extends TestCase
{
    use RefreshDatabase;

    public function test_public_pages_render(): void
    {
        foreach (['/', '/products', '/products/mutton-cloth', '/products/stockinette', '/about', '/insights', '/contact', '/thank-you', '/sitemap.xml'] as $uri) {
            $this->get($uri)->assertOk();
        }
    }

    public function test_an_enquiry_is_validated_stored_and_notified(): void
    {
        Mail::fake();

        $response = $this->post(route('enquiries.store'), [
            'full_name' => 'Asha Rao',
            'company_name' => 'North Market Foods',
            'email' => 'asha@example.com',
            'phone' => '+91 90000 00000',
            'country' => 'India',
            'product_interest' => 'stockinette',
            'enquiry_type' => 'bulk-order',
            'estimated_quantity' => '2,000 rolls monthly',
            'message' => 'Please share current sizes, packaging options and commercial terms.',
            'consent' => '1',
            'source_page' => route('products.stockinette'),
            'website' => '',
        ]);

        $response->assertRedirect(route('enquiries.thank-you'));
        $this->assertDatabaseHas('enquiries', ['email' => 'asha@example.com', 'status' => 'new']);
        Mail::assertSent(EnquiryReceived::class, fn ($mail) => $mail->enquiry->email === 'asha@example.com');
    }

    public function test_required_fields_and_safe_enums_are_enforced(): void
    {
        $this->from(route('contact'))->post(route('enquiries.store'), [
            'full_name' => '',
            'email' => 'invalid',
            'product_interest' => 'invented-product',
            'enquiry_type' => 'anything',
            'message' => 'short',
        ])->assertRedirect(route('contact'))
            ->assertSessionHasErrors(['full_name', 'email', 'product_interest', 'enquiry_type', 'message', 'consent']);

        $this->assertSame(0, Enquiry::count());
    }

    public function test_honeypot_rejects_bots(): void
    {
        $this->post(route('enquiries.store'), [
            'full_name' => 'Bot Name',
            'email' => 'bot@example.com',
            'product_interest' => 'general',
            'enquiry_type' => 'other',
            'message' => 'This automated submission should not be stored.',
            'consent' => '1',
            'website' => 'spam.example',
        ])->assertSessionHasErrors('website');

        $this->assertSame(0, Enquiry::count());
    }

    public function test_product_pages_preselect_the_correct_enquiry_product(): void
    {
        $this->get(route('products.mutton-cloth'))->assertOk()->assertSee('product=mutton-cloth', false);
        $this->get(route('products.stockinette'))->assertOk()->assertSee('product=stockinette', false);

        $this->get(route('contact', ['intent' => 'quote', 'product' => 'stockinette']))
            ->assertOk()
            ->assertSee('value="stockinette" selected', false);
    }
}
