<?php

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;

function createTestUser(array $overrides = []): User
{
    return User::forceCreate(array_merge([
        'firstname' => 'Test',
        'lastname' => 'User',
        'displayname' => 'Test User',
        'email' => 'test+'.Str::uuid().'@example.com',
        'timezone' => 'America/New_York',
        'password' => Hash::make('password'),
    ], $overrides));
}

test('calendar index handles invalid date without error', function () {
    $user = createTestUser();

    $this->actingAs($user)
        ->get(route('calendar.index', ['date' => 'not-a-date']))
        ->assertOk();
});

test('daytime_hours persists to user settings', function () {
    $user = createTestUser();

    $this->actingAs($user)
        ->get(route('calendar.index', ['daytime_hours' => 1]))
        ->assertOk();

    $value = DB::table('user_settings')
        ->where('user_id', $user->id)
        ->where('key', 'calendar.daytime_hours')
        ->value('value');

    expect($value)->toBe('1');
});
