<?php

declare(strict_types=1);

usesApiSuite();

it('loads a single department overview for superusers without department scoped access', function (): void {
    api_test_covers('GET /superuser/departments/{id}/overview', 'happy');

    $department = api_fixtures()->createDepartment([
        'name' => 'Overview Department ' . uniqid('', false),
        'description' => 'Department overview fixture',
        'economic_department_id' => 42,
        'visible' => 1,
    ]);
    $departmentRow = api_fixtures()->fetchRowById('departments', (int)$department['id']);
    $session = api_fixtures()->createUserSession([
        'superuser_fetch_department',
    ]);

    $response = api_client()->get(
        '/superuser/departments/' . $department['id'] . '/overview?date=2026-07-06&date_to=2026-07-06',
        $session['headers']
    );

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

    $payload = $response->data();

    expect($payload)->toBeArray();
    expect($payload['department'])
        ->toBeArray()
        ->toHaveKey('id', (int)$department['id'])
        ->toHaveKey('name', $departmentRow['name'])
        ->toHaveKey('description', 'Department overview fixture')
        ->toHaveKey('economic_department_id', 42);

    expect($payload['overview'])
        ->toBeArray()
        ->toHaveKey('department_ids', [(int)$department['id']])
        ->toHaveKey('date', '2026-07-06')
        ->toHaveKey('date_to', '2026-07-06');

    expect($payload['overview']['metrics'])
        ->toBeArray()
        ->toHaveKeys([
            'bookings',
            'complaints',
            'night_washes',
            'revenue',
            'washes',
            'products_sold',
            'transactions',
            'water_usage',
            'overtime',
        ]);
    expect($payload['overview']['metrics']['revenue']['state'])->toBe('ready');
    expect($payload['overview']['metrics']['revenue']['value'])->toBe(0);
    expect($payload['overview']['products'])->toBeArray();
});

it('rejects superuser department overview requests without permission or valid input', function (): void {
    api_test_covers('GET /superuser/departments/{id}/overview', 'auth');
    api_test_covers('GET /superuser/departments/{id}/overview', 'failure');

    $department = api_fixtures()->createDepartment();
    $unauthorizedSession = api_fixtures()->createUserSession([]);

    api_client()->get(
        '/superuser/departments/' . $department['id'] . '/overview?date=2026-07-06',
        $unauthorizedSession['headers']
    )
        ->assertStatus(403)
        ->assertEnvelope()
        ->assertSuccess(false)
        ->assertMissingPermissions(['superuser_fetch_department']);

    $session = api_fixtures()->createUserSession(['superuser_fetch_department']);

    api_client()->get('/superuser/departments/bad/overview?date=2026-07-06', $session['headers'])
        ->assertStatus(400)
        ->assertEnvelope()
        ->assertSuccess(false)
        ->assertMessage('Parameter id must be a positive integer');

    api_client()->get('/superuser/departments/' . $department['id'] . '/overview', $session['headers'])
        ->assertStatus(400)
        ->assertEnvelope()
        ->assertSuccess(false)
        ->assertMessage('Missing required parameters: date');

    api_client()->get('/superuser/departments/99999999/overview?date=2026-07-06', $session['headers'])
        ->assertStatus(404)
        ->assertEnvelope()
        ->assertSuccess(false)
        ->assertMessage('Department not found');
});
