<?php

declare(strict_types=1);

usesApiSuite();

it('lists limited backoffice permission templates for superuser role maintenance', function (): void {
    api_test_covers('GET /roles/limited-backoffice-permission-templates', 'happy');

    $session = api_fixtures()->createUserSession([
        'superuser',
        'add_role_permission',
    ]);

    $response = api_client()->get('/roles/limited-backoffice-permission-templates', $session['headers']);

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

    $templates = $response->data();
    expect(array_column($templates, 'key'))->toBe([
        'viewer',
        'cashier',
        'booking_coordinator',
        'operations_lead',
        'department_admin',
    ]);

    $templatesByKey = array_column($templates, null, 'key');
    expect($templatesByKey['cashier']['permissions'] ?? [])->toContain('list_department_daily_reports');
    expect($templatesByKey['cashier']['permissions'] ?? [])->toContain('list_notifications');
    expect($templatesByKey['cashier']['permissions'] ?? [])->toContain('statistics_orders_new');
    expect($templatesByKey['department_admin']['permissions'] ?? [])->toContain('limited_backoffice_access');
    expect($templatesByKey['department_admin']['permissions'] ?? [])->toContain('limited_backoffice_prices_manage');
    expect($templatesByKey['department_admin']['permissions'] ?? [])->toContain('limited_backoffice_customer_pricing_view');
    expect($templatesByKey['department_admin']['permissions'] ?? [])->toContain('limited_backoffice_customer_pricing_manage');
    expect($templatesByKey['department_admin']['permissions'] ?? [])->toContain('limited_backoffice_employees_manage');
});

it('requires superuser and role permission edit access for limited backoffice permission templates', function (): void {
    api_test_covers('GET /roles/limited-backoffice-permission-templates', 'auth');

    api_client()
        ->get('/roles/limited-backoffice-permission-templates', api_fixtures()->createUserSession(['add_role_permission'])['headers'])
        ->assertStatus(403)
        ->assertEnvelope()
        ->assertSuccess(false)
        ->assertMissingPermissions(['superuser']);

    api_client()
        ->get('/roles/limited-backoffice-permission-templates', api_fixtures()->createUserSession(['superuser'])['headers'])
        ->assertStatus(403)
        ->assertEnvelope()
        ->assertSuccess(false)
        ->assertMissingPermissions(['add_role_permission']);
});
