<?php

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

test('humans only toggle excludes bot scans from the total', function () {
    $admin = User::factory()->admin()->create();
    $qrCode = QrCode::factory()->create();

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

    $this->actingAs($admin);

    $humansOnly = Livewire::test('pages::admin.qr-codes.show', ['qrCode' => $qrCode]);
    expect($humansOnly->get('stats')['total'])->toBe(3);

    $all = Livewire::test('pages::admin.qr-codes.show', ['qrCode' => $qrCode])
        ->set('humansOnly', false);
    expect($all->get('stats')['total'])->toBe(5);
});

test('unique visitor count is distinct on visitor hash', function () {
    $admin = User::factory()->admin()->create();
    $qrCode = QrCode::factory()->create();

    QrCodeScan::factory()->count(2)->create([
        'qr_code_id' => $qrCode->id,
        'is_bot' => false,
        'visitor_hash' => 'same-visitor-hash',
        'scanned_at' => now(),
    ]);
    QrCodeScan::factory()->create([
        'qr_code_id' => $qrCode->id,
        'is_bot' => false,
        'visitor_hash' => 'another-visitor-hash',
        'scanned_at' => now(),
    ]);

    $this->actingAs($admin);

    $component = Livewire::test('pages::admin.qr-codes.show', ['qrCode' => $qrCode]);

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

test('the date range filter narrows scans', function () {
    $admin = User::factory()->admin()->create();
    $qrCode = QrCode::factory()->create();

    QrCodeScan::factory()->create(['qr_code_id' => $qrCode->id, 'is_bot' => false, 'scanned_at' => now()->subDays(2)]);
    QrCodeScan::factory()->create(['qr_code_id' => $qrCode->id, 'is_bot' => false, 'scanned_at' => now()->subDays(45)]);

    $this->actingAs($admin);

    $sevenDays = Livewire::test('pages::admin.qr-codes.show', ['qrCode' => $qrCode])
        ->set('range', '7');
    expect($sevenDays->get('stats')['total'])->toBe(1);

    $allTime = Livewire::test('pages::admin.qr-codes.show', ['qrCode' => $qrCode])
        ->set('range', 'all');
    expect($allTime->get('stats')['total'])->toBe(2);
});

test('a new qr code shows the empty scans state', function () {
    $admin = User::factory()->admin()->create();
    $qrCode = QrCode::factory()->create();

    $this->actingAs($admin);

    Livewire::test('pages::admin.qr-codes.show', ['qrCode' => $qrCode])
        ->assertSee(__('No scans yet'));
});

test('the active toggle updates the qr code', function () {
    $admin = User::factory()->admin()->create();
    $qrCode = QrCode::factory()->create(['is_active' => true]);

    $this->actingAs($admin);

    Livewire::test('pages::admin.qr-codes.show', ['qrCode' => $qrCode])
        ->call('toggleActive')
        ->assertHasNoErrors();

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

test('scans pending enrichment are counted and excluded from breakdowns', function () {
    $admin = User::factory()->admin()->create();
    $qrCode = QrCode::factory()->create();

    QrCodeScan::factory()->create([
        'qr_code_id' => $qrCode->id,
        'is_bot' => false,
        'enriched_at' => null,
        'device_type' => null,
        'country' => null,
        'scanned_at' => now(),
    ]);
    QrCodeScan::factory()->desktop()->create([
        'qr_code_id' => $qrCode->id,
        'is_bot' => false,
        'scanned_at' => now(),
    ]);

    $this->actingAs($admin);

    $component = Livewire::test('pages::admin.qr-codes.show', ['qrCode' => $qrCode]);

    expect($component->get('pendingEnrichmentCount'))->toBe(1);
    expect($component->get('byDeviceType'))->toHaveCount(1);
});
