<?php

use App\Enums\QrCodeType;
use App\Models\QrCode;
use App\Models\QrCodeScan;
use App\Models\User;
use Livewire\Livewire;

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

    $active = QrCode::factory()->count(2)->create(['is_active' => true]);
    QrCode::factory()->inactive()->create();

    QrCodeScan::factory()->count(3)->create(['qr_code_id' => $active->first()->id, 'is_bot' => false, 'scanned_at' => now()->subDay()]);
    QrCodeScan::factory()->count(2)->create(['qr_code_id' => $active->first()->id, 'is_bot' => false, 'scanned_at' => now()]);
    QrCodeScan::factory()->bot()->create(['qr_code_id' => $active->first()->id, 'scanned_at' => now()]);

    $this->actingAs($admin);

    $component = Livewire::test('pages::admin.qr-codes.index');

    expect($component->get('stats'))->toMatchArray([
        'total' => 3,
        'active' => 2,
        'scans' => 5,
        'today' => 2,
    ]);
});

test('qr code listing search matches by name, short code, and target url', function () {
    $admin = User::factory()->admin()->create();

    QrCode::factory()->create(['name' => 'Front Window Poster', 'short_code' => 'FRONTWIN']);
    QrCode::factory()->create(['name' => 'Back Door Flyer', 'short_code' => 'BACKDOOR']);

    $this->actingAs($admin);

    $component = Livewire::test('pages::admin.qr-codes.index')->set('search', 'Front Window');
    expect($component->get('codes')->total())->toBe(1);
    $component->assertSee('Front Window Poster')->assertDontSee('Back Door Flyer');

    $byShortCode = Livewire::test('pages::admin.qr-codes.index')->set('search', 'BACKDOOR');
    expect($byShortCode->get('codes')->total())->toBe(1);
});

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

    QrCode::factory()->url()->create(['name' => 'Url Code']);
    QrCode::factory()->static()->create(['name' => 'Static Code']);

    $this->actingAs($admin);

    $component = Livewire::test('pages::admin.qr-codes.index')->set('typeFilter', QrCodeType::Text->value);

    expect($component->get('codes')->total())->toBe(1);
    $component->assertSee('Static Code')->assertDontSee('Url Code');
});

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

    QrCode::factory()->create(['name' => 'Enabled Poster', 'is_active' => true]);
    QrCode::factory()->inactive()->create(['name' => 'Suspended Poster']);

    $this->actingAs($admin);

    $component = Livewire::test('pages::admin.qr-codes.index')->set('statusFilter', 'inactive');

    expect($component->get('codes')->total())->toBe(1);
    $component->assertSee('Suspended Poster')->assertDontSee('Enabled Poster');
});

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

    $this->actingAs($admin);

    Livewire::test('pages::admin.qr-codes.index')
        ->assertSee(__('No QR codes found'));
});

test('a qr code can be toggled active from the listing page', function () {
    $admin = User::factory()->admin()->create();
    $qrCode = QrCode::factory()->create(['is_active' => true]);

    $this->actingAs($admin);

    Livewire::test('pages::admin.qr-codes.index')
        ->call('toggleActive', $qrCode->id)
        ->assertHasNoErrors();

    expect($qrCode->fresh()->is_active)->toBeFalse();
});

test('soft deleting a qr code removes it from the listing', function () {
    $admin = User::factory()->admin()->create();
    $qrCode = QrCode::factory()->create(['name' => 'Doomed Code']);

    $this->actingAs($admin);

    Livewire::test('pages::admin.qr-codes.index')
        ->call('delete', $qrCode->id)
        ->assertHasNoErrors()
        ->assertDontSee('Doomed Code');

    expect(QrCode::query()->find($qrCode->id))->toBeNull();
    expect(QrCode::withTrashed()->find($qrCode->id)->trashed())->toBeTrue();
});
