<?php

declare(strict_types=1);

use App\Support\Settings\SettingsRegistry;
use Illuminate\Support\Facades\Validator;

it('registers findings sla_days with defaults', function (): void {
    $definition = app(SettingsRegistry::class)->require('findings', 'sla_days');

    expect($definition->type)->toBe('json')
        ->and($definition->systemDefault)->toBe([
            'critical' => 3,
            'high' => 7,
            'medium' => 14,
            'low' => 30,
        ]);
});

it('validates findings sla_days values and normalizes them', function (): void {
    $definition = app(SettingsRegistry::class)->require('findings', 'sla_days');

    $valid = [
        'medium' => '14',
        'critical' => '3',
        'low' => 30,
        'high' => 7,
    ];

    $validator = Validator::make(
        ['value' => $valid],
        ['value' => $definition->rules],
    );

    expect($validator->fails())->toBeFalse();
    expect($definition->normalize($validator->validated()['value']))->toBe([
        'critical' => 3,
        'high' => 7,
        'medium' => 14,
        'low' => 30,
    ]);
});

it('rejects malformed findings sla_days values', function (): void {
    $definition = app(SettingsRegistry::class)->require('findings', 'sla_days');

    $invalidValue = Validator::make(
        ['value' => ['critical' => 0, 'high' => 7, 'medium' => 14, 'low' => 30]],
        ['value' => $definition->rules],
    );

    $unknownSeverity = Validator::make(
        ['value' => ['critical' => 3, 'high' => 7, 'medium' => 14, 'low' => 30, 'urgent' => 1]],
        ['value' => $definition->rules],
    );

    expect($invalidValue->fails())->toBeTrue()
        ->and($unknownSeverity->fails())->toBeTrue();
});

it('allows partial findings sla_days and stores only provided keys', function (): void {
    $definition = app(SettingsRegistry::class)->require('findings', 'sla_days');

    $partial = ['critical' => 2];

    $validator = Validator::make(
        ['value' => $partial],
        ['value' => $definition->rules],
    );

    expect($validator->fails())->toBeFalse();
    expect($definition->normalize($validator->validated()['value']))->toBe([
        'critical' => 2,
    ]);
});
