<?php

declare(strict_types=1);

usesApiSuite();

it('lists only visible active departments with public time bookings enabled', function (): void {
    api_test_covers('GET /department/timebookings/departments/public', 'happy');

    $enabledDepartment = api_fixtures()->createDepartment([
        'name' => 'Public Time Booking Enabled ' . uniqid('', false),
        'description' => 'Enabled booking department address',
        'visible' => 1,
        'archived' => 0,
        'latitude' => 55.1,
        'longitude' => 12.1,
        'order_priority' => 7,
    ]);
    api_fixtures()->setDepartmentTimeBookingsEnabled((int)$enabledDepartment['id'], true);

    $disabledDepartment = api_fixtures()->createDepartment([
        'name' => 'Public Time Booking Disabled ' . uniqid('', false),
        'visible' => 1,
        'archived' => 0,
    ]);
    api_fixtures()->setDepartmentTimeBookingsEnabled((int)$disabledDepartment['id'], false);

    $missingVariableDepartment = api_fixtures()->createDepartment([
        'name' => 'Public Time Booking Missing Variable ' . uniqid('', false),
        'visible' => 1,
        'archived' => 0,
    ]);

    $legacyTruthyDepartment = api_fixtures()->createDepartment([
        'name' => 'Public Time Booking Legacy Truthy ' . uniqid('', false),
        'visible' => 1,
        'archived' => 0,
    ]);
    api_fixtures()->setDepartmentVariable(
        (int)$legacyTruthyDepartment['id'],
        'bookingsystem_time_based_enabled',
        '1'
    );

    $hiddenDepartment = api_fixtures()->createDepartment([
        'name' => 'Public Time Booking Hidden ' . uniqid('', false),
        'visible' => 0,
        'archived' => 0,
    ]);
    api_fixtures()->setDepartmentTimeBookingsEnabled((int)$hiddenDepartment['id'], true);

    $archivedDepartment = api_fixtures()->createDepartment([
        'name' => 'Public Time Booking Archived ' . uniqid('', false),
        'visible' => 1,
        'archived' => 1,
    ]);
    api_fixtures()->setDepartmentTimeBookingsEnabled((int)$archivedDepartment['id'], true);

    $response = api_client()->get('/department/timebookings/departments/public');

    $response
        ->assertStatus(200)
        ->assertEnvelope()
        ->assertSuccess();

    $departmentsById = [];
    foreach ($response->data() as $department) {
        $departmentsById[(int)($department['id'] ?? 0)] = $department;
    }

    expect($departmentsById)
        ->toHaveKey((int)$enabledDepartment['id'])
        ->not->toHaveKey((int)$disabledDepartment['id'])
        ->not->toHaveKey((int)$missingVariableDepartment['id'])
        ->not->toHaveKey((int)$legacyTruthyDepartment['id'])
        ->not->toHaveKey((int)$hiddenDepartment['id'])
        ->not->toHaveKey((int)$archivedDepartment['id']);

    $returnedEnabledDepartment = $departmentsById[(int)$enabledDepartment['id']];
    expect($returnedEnabledDepartment)
        ->toHaveKey('name')
        ->toHaveKey('description', 'Enabled booking department address')
        ->toHaveKey('address', 'Enabled booking department address')
        ->toHaveKey('time_booking_enabled', true)
        ->not->toHaveKey('slack_webhook')
        ->not->toHaveKey('custom_pricing_only')
        ->not->toHaveKey('variables')
        ->not->toHaveKey('bookingsystem_time_based_enabled');
});

it('returns an empty public time-booking department list when no matching department is enabled', function (): void {
    api_test_covers('GET /department/timebookings/departments/public', 'empty');

    $uniqueName = 'Public Time Booking Empty ' . uniqid('', false);
    $disabledDepartment = api_fixtures()->createDepartment([
        'name' => $uniqueName,
        'visible' => 1,
        'archived' => 0,
    ]);
    api_fixtures()->setDepartmentTimeBookingsEnabled((int)$disabledDepartment['id'], false);

    $response = api_client()->get(
        '/department/timebookings/departments/public?search=' . rawurlencode($uniqueName)
    );

    $response
        ->assertStatus(200)
        ->assertEnvelope()
        ->assertSuccess();

    expect($response->data())->toBe([]);
});

it('rejects public time-booking detail requests for disabled departments', function (): void {
    api_test_covers('GET /department/timebookings/types/public', 'disabled');

    $disabledDepartment = api_fixtures()->createDepartment([
        'name' => 'Disabled Public Time Booking Detail ' . uniqid('', false),
        'visible' => 1,
        'archived' => 0,
    ]);
    api_fixtures()->setDepartmentTimeBookingsEnabled((int)$disabledDepartment['id'], false);

    api_client()->get('/department/timebookings/types/public?id=' . (int)$disabledDepartment['id'])
        ->assertStatus(404)
        ->assertEnvelope()
        ->assertSuccess(false)
        ->assertMessage('Department time bookings are not enabled');
});
