<?php

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

test('creating a url qr code persists correctly and generates a short code', function () {
    $admin = User::factory()->admin()->create();

    $this->actingAs($admin);

    Livewire::test('pages::admin.qr-codes.create')
        ->set('name', 'Front Window Poster')
        ->set('type', QrCodeType::Url->value)
        ->set('targetUrl', 'https://example.com/spring-sale')
        ->set('isDynamic', true)
        ->call('save')
        ->assertHasNoErrors();

    $qrCode = QrCode::query()->where('name', 'Front Window Poster')->firstOrFail();

    expect($qrCode->type)->toBe(QrCodeType::Url);
    expect($qrCode->target_url)->toBe('https://example.com/spring-sale');
    expect($qrCode->is_dynamic)->toBeTrue();
    expect($qrCode->payload)->toBeNull();
    expect($qrCode->short_code)->not->toBeEmpty();
    expect(strlen($qrCode->short_code))->toBe(8);
});

test('creating a wifi qr code forces is_dynamic to false', function () {
    $admin = User::factory()->admin()->create();

    $this->actingAs($admin);

    Livewire::test('pages::admin.qr-codes.create')
        ->set('name', 'Guest WiFi')
        ->set('type', QrCodeType::Wifi->value)
        ->set('wifiSsid', 'PushpVatikaGuest')
        ->set('wifiPassword', 'flowers123')
        ->set('wifiEncryption', 'WPA')
        // A malicious or buggy client could still send isDynamic=true; the
        // server must ignore it for non-URL types.
        ->set('isDynamic', true)
        ->call('save')
        ->assertHasNoErrors();

    $qrCode = QrCode::query()->where('name', 'Guest WiFi')->firstOrFail();

    expect($qrCode->type)->toBe(QrCodeType::Wifi);
    expect($qrCode->is_dynamic)->toBeFalse();
    expect($qrCode->target_url)->toBeNull();
    expect($qrCode->payload)->toMatchArray([
        'ssid' => 'PushpVatikaGuest',
        'password' => 'flowers123',
        'encryption' => 'WPA',
        'hidden' => false,
    ]);
});

test('a url qr code requires a valid target url', function () {
    $admin = User::factory()->admin()->create();

    $this->actingAs($admin);

    Livewire::test('pages::admin.qr-codes.create')
        ->set('name', 'Bad URL Code')
        ->set('type', QrCodeType::Url->value)
        ->set('targetUrl', 'not-a-url')
        ->call('save')
        ->assertHasErrors(['targetUrl']);
});

test('creating a text qr code stores the payload', function () {
    $admin = User::factory()->admin()->create();

    $this->actingAs($admin);

    Livewire::test('pages::admin.qr-codes.create')
        ->set('name', 'Care Instructions')
        ->set('type', QrCodeType::Text->value)
        ->set('text', 'Trim stems and change water every two days.')
        ->call('save')
        ->assertHasNoErrors();

    $qrCode = QrCode::query()->where('name', 'Care Instructions')->firstOrFail();

    expect($qrCode->type)->toBe(QrCodeType::Text);
    expect($qrCode->is_dynamic)->toBeFalse();
    expect($qrCode->payload)->toBe(['text' => 'Trim stems and change water every two days.']);
});
