<?php

use App\Models\User;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Modules\AiAgent\Filament\Pages\ManageFaqs;
use Spatie\Permission\Models\Permission;

beforeEach(function () {
    config([
        'services.bnfexpress.ai_api_url' => 'https://bnfexpress.test',
        'services.bnfexpress.client_id' => 'ev_admin',
        'services.bnfexpress.client_secret' => 'test-secret',
    ]);

    Permission::findOrCreate('manage_ai_agent', 'web');

    $this->admin = User::factory()->create()->givePermissionTo(['manage_ai_agent']);
    $this->actingAs($this->admin);
});

test('a user without manage_ai_agent cannot access it', function () {
    $this->actingAs(User::factory()->create());

    expect(ManageFaqs::canAccess())->toBeFalse();
});

test('it renders and lists faqs from the client', function () {
    Http::fake(['bnfexpress.test/*' => Http::response([
        'total' => 1,
        'faqs' => [
            ['id' => 1, 'content' => 'How do I charge my EV?', 'metadata' => []],
        ],
    ])]);

    Livewire::test(ManageFaqs::class)
        ->assertOk()
        ->loadTable()
        ->assertSee('How do I charge my EV?');

    Http::assertSent(fn (Request $request) => str_contains($request->url(), '/admin/faqs') && $request->method() === 'GET');
});

test('creating a faq calls createFaq and shows a success notification', function () {
    Http::fake(['bnfexpress.test/*' => fn (Request $request) => match ($request->method()) {
        'POST' => Http::response(['id' => 1, 'content' => 'New FAQ', 'metadata' => []], 201),
        default => Http::response(['total' => 0, 'faqs' => []]),
    }]);

    Livewire::test(ManageFaqs::class)
        ->loadTable()
        ->callTableAction('create', data: ['content' => 'New FAQ', 'metadata' => []])
        ->assertNotified('FAQ created');

    Http::assertSent(fn (Request $request) => $request->method() === 'POST' && $request['content'] === 'New FAQ');
});

test('a failed create surfaces the gateway detail message', function () {
    Http::fake(['bnfexpress.test/*' => fn (Request $request) => match ($request->method()) {
        'POST' => Http::response(['detail' => 'Content is required.'], 422),
        default => Http::response(['total' => 0, 'faqs' => []]),
    }]);

    Livewire::test(ManageFaqs::class)
        ->loadTable()
        ->callTableAction('create', data: ['content' => 'New FAQ', 'metadata' => []])
        ->assertNotified('Failed to create FAQ');
});

test('deleting a faq calls deleteFaq and shows a success notification', function () {
    Http::fake(['bnfexpress.test/*' => fn (Request $request) => match ($request->method()) {
        'DELETE' => Http::response(['deleted' => true]),
        default => Http::response(['total' => 1, 'faqs' => [['id' => 1, 'content' => 'To delete', 'metadata' => []]]]),
    }]);

    Livewire::test(ManageFaqs::class)
        ->loadTable()
        ->callTableAction('delete', 1)
        ->assertNotified('FAQ deleted');

    Http::assertSent(fn (Request $request) => $request->method() === 'DELETE' && str_contains($request->url(), '/admin/faqs/1'));
});
