<?php

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

test('contact detail page displays the submission', function () {
    $admin = User::factory()->admin()->create();
    $contactSubmission = ContactSubmission::factory()->create([
        'name' => 'Priya Sharma',
        'message' => "Line one\nLine two",
    ]);

    $this->actingAs($admin);

    Livewire::test('pages::admin.contact-detail', ['contactSubmission' => $contactSubmission])
        ->assertSee('Priya Sharma')
        ->assertSee($contactSubmission->phone)
        ->assertSee($contactSubmission->email);
});

test('status update persists', function () {
    $admin = User::factory()->admin()->create();
    $contactSubmission = ContactSubmission::factory()->newSubmission()->create();

    $this->actingAs($admin);

    Livewire::test('pages::admin.contact-detail', ['contactSubmission' => $contactSubmission])
        ->set('status', ContactStatus::Converted->value)
        ->call('updateStatus')
        ->assertHasNoErrors();

    expect($contactSubmission->fresh()->status)->toBe(ContactStatus::Converted);
});

test('status update rejects an invalid status value', function () {
    $admin = User::factory()->admin()->create();
    $contactSubmission = ContactSubmission::factory()->newSubmission()->create();

    $this->actingAs($admin);

    Livewire::test('pages::admin.contact-detail', ['contactSubmission' => $contactSubmission])
        ->set('status', 'not-a-real-status')
        ->call('updateStatus')
        ->assertHasErrors(['status']);

    expect($contactSubmission->fresh()->status)->toBe(ContactStatus::New);
});
