import 'package:flutter_test/flutter_test.dart';

import 'package:superport_v2/core/permissions/permission_manager.dart';
import 'package:superport_v2/core/permissions/permission_resources.dart';

void main() {
  group('PermissionManager', () {
    test('서버 권한을 적용하면 해당 리소스 권한이 설정된다', () {
      final manager = PermissionManager();

      manager.applyServerPermissions({
        PermissionResources.stockTransactions: {
          PermissionAction.view,
          PermissionAction.create,
        },
      });

      expect(
        manager.can(
          PermissionResources.stockTransactions,
          PermissionAction.view,
        ),
        isTrue,
      );
      expect(
        manager.can(
          PermissionResources.stockTransactions,
          PermissionAction.create,
        ),
        isTrue,
      );
      expect(
        manager.can(
          PermissionResources.stockTransactions,
          PermissionAction.edit,
        ),
        isFalse,
      );
    });

    test('오버라이드가 서버 권한보다 우선한다', () {
      final manager = PermissionManager();

      manager.applyServerPermissions({
        PermissionResources.approvalSteps: {PermissionAction.view},
      });
      manager.updateOverrides({
        PermissionResources.approvalSteps: {PermissionAction.edit},
      });

      expect(
        manager.can(PermissionResources.approvalSteps, PermissionAction.edit),
        isTrue,
      );
      expect(
        manager.can(PermissionResources.approvalSteps, PermissionAction.view),
        isFalse,
      );
    });

    test('서버 권한을 초기화하면 환경 설정으로 되돌아간다', () {
      final manager = PermissionManager();
      manager.applyServerPermissions({
        PermissionResources.vendors: {PermissionAction.view},
      });
      expect(
        manager.can(PermissionResources.vendors, PermissionAction.edit),
        isFalse,
      );

      manager.clearServerPermissions();

      // 환경 설정에 권한이 명시되지 않으면 기본적으로 허용한다.
      expect(
        manager.can(PermissionResources.vendors, PermissionAction.edit),
        isTrue,
      );
    });

    test('별칭 경로도 normalize되어 권한을 확인한다', () {
      final manager = PermissionManager();
      manager.applyServerPermissions({
        PermissionResources.stockTransactions: {PermissionAction.view},
      });

      expect(
        manager.can('/Inventory/Inbound?page=1', PermissionAction.view),
        isTrue,
      );
      expect(
        manager.can('/inventory/outbound', PermissionAction.edit),
        isFalse,
      );
    });

    test('scope 리소스도 권한 검사에 활용된다', () {
      final manager = PermissionManager();
      manager.applyServerPermissions({
        PermissionResources.inventoryScope: {PermissionAction.view},
      });

      expect(
        manager.can(PermissionResources.inventoryScope, PermissionAction.view),
        isTrue,
      );
      expect(
        manager.can(PermissionResources.inventoryScope, PermissionAction.edit),
        isFalse,
      );
    });

    test('scope 권한이 없으면 기본적으로 거부된다', () {
      final manager = PermissionManager();

      expect(
        manager.can(PermissionResources.inventoryScope, PermissionAction.view),
        isFalse,
      );
    });
  });
}
