<?php

use App\Enums\ContactStatus;
use App\Models\ContactSubmission;
use App\Models\User;
use Livewire\Livewire;

test('contacts listing shows correct stat counts', function () {
    $admin = User::factory()->admin()->create();

    ContactSubmission::factory()->newSubmission()->count(2)->create(['created_at' => now()->subDays(2)]);
    ContactSubmission::factory()->contacted()->count(1)->create(['created_at' => now()->subDays(2)]);
    ContactSubmission::factory()->converted()->count(3)->create(['created_at' => now()->subDays(2)]);
    ContactSubmission::factory()->newSubmission()->create(['created_at' => now()]);

    $this->actingAs($admin);

    $component = Livewire::test('pages::admin.contacts');

    expect($component->get('stats'))->toMatchArray([
        'total' => 7,
        'new' => 3,
        'contacted' => 1,
        'converted' => 3,
        'today' => 1,
    ]);
});

test('contacts listing status filter narrows results', function () {
    $admin = User::factory()->admin()->create();

    ContactSubmission::factory()->newSubmission()->create(['name' => 'New Person']);
    ContactSubmission::factory()->converted()->create(['name' => 'Converted Person']);

    $this->actingAs($admin);

    $component = Livewire::test('pages::admin.contacts')
        ->set('status', 'converted');

    expect($component->get('submissions')->total())->toBe(1);
    $component->assertSee('Converted Person')->assertDontSee('New Person');
});

test('contacts listing falls back to all for an unrecognised status', function () {
    $admin = User::factory()->admin()->create();

    ContactSubmission::factory()->newSubmission()->create();
    ContactSubmission::factory()->converted()->create();

    $this->actingAs($admin);

    $component = Livewire::test('pages::admin.contacts')
        ->set('status', 'not-a-real-status');

    expect($component->get('submissions')->total())->toBe(2);
});

test('contacts listing search matches by name', function () {
    $admin = User::factory()->admin()->create();

    ContactSubmission::factory()->create(['name' => 'Priya Sharma']);
    ContactSubmission::factory()->create(['name' => 'Rohan Mehta']);

    $this->actingAs($admin);

    $component = Livewire::test('pages::admin.contacts')
        ->set('search', 'Priya');

    expect($component->get('submissions')->total())->toBe(1);
    $component->assertSee('Priya Sharma')->assertDontSee('Rohan Mehta');
});

test('contacts listing search matches by email', function () {
    $admin = User::factory()->admin()->create();

    ContactSubmission::factory()->create(['name' => 'Priya Sharma', 'email' => 'priya@example.com']);
    ContactSubmission::factory()->create(['name' => 'Rohan Mehta', 'email' => 'rohan@example.com']);

    $this->actingAs($admin);

    $component = Livewire::test('pages::admin.contacts')
        ->set('search', 'rohan@example.com');

    expect($component->get('submissions')->total())->toBe(1);
    $component->assertSee('Rohan Mehta')->assertDontSee('Priya Sharma');
});

test('contacts listing shows an empty state when there are no submissions', function () {
    $admin = User::factory()->admin()->create();

    $this->actingAs($admin);

    Livewire::test('pages::admin.contacts')
        ->assertSee(__('No contact submissions found'));
});
