<?php

namespace Tests\Feature;

use App\Models\Lead;
use App\Models\LeadFollowup;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class WebsiteContactLeadTest extends TestCase
{
    use RefreshDatabase;

    public function test_contact_form_creates_superadmin_lead(): void
    {
        $response = $this->post(route('website.contact.submit'), [
            'name' => 'Jane Doe',
            'email' => 'jane@example.com',
            'phone' => '9876543210',
            'subject' => 'Book a demo',
            'message' => 'We need HRMS for 40 employees.',
        ]);

        $response->assertRedirect();
        $response->assertSessionHas('success');

        $this->assertDatabaseHas('leads', [
            'contact_person_name' => 'Jane Doe',
            'mobile' => '9876543210',
            'email' => 'jane@example.com',
            'source' => 'website_contact',
            'status' => 'new',
        ]);

        $lead = Lead::where('mobile', '9876543210')->first();
        $this->assertStringContainsString('Book a demo', $lead->notes);
        $this->assertStringContainsString('40 employees', $lead->notes);

        $this->assertDatabaseHas('lead_followups', [
            'lead_id' => $lead->id,
            'status' => 'pending',
        ]);
        $this->assertInstanceOf(LeadFollowup::class, LeadFollowup::where('lead_id', $lead->id)->first());
    }

    public function test_contact_form_requires_phone(): void
    {
        $response = $this->post(route('website.contact.submit'), [
            'name' => 'Jane Doe',
            'email' => 'jane@example.com',
            'subject' => 'Book a demo',
            'message' => 'Hello',
        ]);

        $response->assertSessionHasErrors(['phone']);
        $this->assertDatabaseCount('leads', 0);
    }
}
