<?php

use App\Filament\Resources\BackupScheduleResource\Pages\CreateBackupSchedule;
use Filament\Facades\Filament;
use Livewire\Livewire;

test('weekly schedules require at least one day of week', function () {
    [$user, $tenant] = createUserWithTenant(role: 'manager');

    $this->actingAs($user);
    Filament::setTenant($tenant, true);

    Livewire::test(CreateBackupSchedule::class)
        ->fillForm([
            'name' => 'Weekly schedule',
            'is_enabled' => true,
            'timezone' => 'UTC',
            'frequency' => 'weekly',
            'time_of_day' => '10:00',
            'days_of_week' => [],
            'policy_types' => ['deviceConfiguration'],
            'include_foundations' => true,
            'retention_keep_last' => 30,
        ])
        ->call('create')
        ->assertHasFormErrors(['days_of_week']);
});

test('unknown policy types are rejected at save time', function () {
    [$user, $tenant] = createUserWithTenant(role: 'manager');

    $this->actingAs($user);
    Filament::setTenant($tenant, true);

    Livewire::test(CreateBackupSchedule::class)
        ->fillForm([
            'name' => 'Invalid policy type schedule',
            'is_enabled' => true,
            'timezone' => 'UTC',
            'frequency' => 'daily',
            'time_of_day' => '10:00',
            'policy_types' => ['definitelyNotARealPolicyType'],
            'include_foundations' => true,
            'retention_keep_last' => 30,
        ])
        ->call('create')
        ->assertHasFormErrors(['policy_types']);
});
