<?php

use App\Services\Calendar\CalendarViewBuilder;
use Carbon\Carbon;

test('timeSlots returns full day when no daytime hours', function () {
    $builder = new CalendarViewBuilder();
    $tz = 'America/New_York';
    $dayStart = Carbon::create(2026, 2, 4, 0, 0, 0, $tz);

    $slots = $builder->timeSlots($dayStart, $tz, '12');

    expect($slots)->toHaveCount(96);
    expect($slots[0]['key'])->toBe('00:00');
    expect($slots[95]['key'])->toBe('23:45');
});

test('timeSlots respects daytime hours', function () {
    $builder = new CalendarViewBuilder();
    $tz = 'America/New_York';
    $dayStart = Carbon::create(2026, 2, 4, 0, 0, 0, $tz);

    $slots = $builder->timeSlots($dayStart, $tz, '12', ['start' => 8, 'end' => 18]);

    expect($slots)->toHaveCount(40);
    expect($slots[0]['key'])->toBe('08:00');
    expect($slots[39]['key'])->toBe('17:45');
});

test('nowIndicator uses minute-accurate offset in daytime hours', function () {
    $builder = new CalendarViewBuilder();
    $tz = 'America/New_York';

    Carbon::setTestNow(Carbon::create(2026, 2, 4, 11, 37, 0, $tz));

    $range = [
        'start' => Carbon::create(2026, 2, 4, 0, 0, 0, $tz),
        'end' => Carbon::create(2026, 2, 4, 23, 59, 59, $tz),
    ];

    $now = $builder->nowIndicator('day', $range, $tz, 15, 1, ['start' => 8, 'end' => 18]);

    expect($now['show'])->toBeTrue();
    expect($now['row'])->toBe(15);
    expect($now['offset'])->toBeGreaterThan(0.46);
    expect($now['offset'])->toBeLessThan(0.47);
    expect($now['col_start'])->toBe(1);

    Carbon::setTestNow();
});
