<?php

declare(strict_types=1);

use App\Http\Controllers\WebAuthn\WebAuthnLoginController;
use App\Http\Controllers\WebAuthn\WebAuthnRegisterController;
use App\Services\SystemConfig\SystemConfigService;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Schema;

beforeEach(function () {
    $ref = new ReflectionClass(SystemConfigService::class);
    $prop = $ref->getProperty('resolvedSettings');
    $prop->setAccessible(true);
    $prop->setValue(null, null);
    Cache::flush();
});

test('WebAuthn login controller class exists and has required methods', function () {
    $ref = new ReflectionClass(WebAuthnLoginController::class);
    expect($ref->hasMethod('options'))->toBeTrue();
    expect($ref->hasMethod('login'))->toBeTrue();
});

test('WebAuthn register controller class exists and has required methods', function () {
    $ref = new ReflectionClass(WebAuthnRegisterController::class);
    expect($ref->hasMethod('options'))->toBeTrue();
    expect($ref->hasMethod('register'))->toBeTrue();
});

test('webauthn_enabled setting defaults to false in fresh DB', function () {
    expect(get_setting('webauthn_enabled', false))->toBeFalse();
});

test('webauthn_enabled setting can be toggled on', function () {
    app(SystemConfigService::class)->update(['webauthn_enabled' => true]);

    expect(get_setting('webauthn_enabled', false))->toBeTrue();
});

test('webauthn_credentials migration created the laragear table', function () {
    expect(Schema::hasTable('webauthn_credentials'))->toBeTrue();
});

test('webauthn_credentials table has the expected key columns', function () {
    $cols = Schema::getColumnListing('webauthn_credentials');

    foreach (['id', 'authenticatable_id', 'authenticatable_type'] as $required) {
        expect($cols)->toContain($required);
    }
});
