import 'package:flutter_test/flutter_test.dart';

import 'package:superport_v2/features/auth/data/dtos/auth_session_dto.dart';

void main() {
  group('AuthSessionDto', () {
    test('permission_codes를 scope 권한으로 변환한다', () {
      final dto = AuthSessionDto.fromJson({
        'access_token': 'access',
        'refresh_token': 'refresh',
        'user': {'id': 1, 'name': '테스터'},
        'permission_codes': [
          'inventory.view',
          'scope:approval.manage',
          ' APPROVAL.VIEW_ALL ',
        ],
      });

      final scopeResources = dto.permissions.map((p) => p.resource).where((r) {
        return r.startsWith('scope:');
      }).toSet();

      expect(
        scopeResources,
        containsAll({
          'scope:inventory.view',
          'scope:approval.manage',
          'scope:approval.view_all',
        }),
      );
    });

    test('permission_scopes 응답도 scope 권한으로 적용한다', () {
      final dto = AuthSessionDto.fromJson({
        'access_token': 'access',
        'refresh_token': 'refresh',
        'user': {'id': 10, 'name': '권한계정'},
        'permissions': [
          {
            'resource': '/dashboard',
            'actions': ['view'],
          },
        ],
        'permission_scopes': [
          {'scope_code': 'inventory.view'},
          {'code': 'approval.view_all'},
          {'scope': 'approval.approve'},
        ],
        'group_permission_scopes': [
          'scope:report.export',
          {'name': 'report.view'},
        ],
      });

      final scopeResources = dto.permissions.map((p) => p.resource).where((r) {
        return r.startsWith('scope:');
      }).toSet();

      expect(
        scopeResources,
        containsAll({
          'scope:inventory.view',
          'scope:approval.view_all',
          'scope:approval.approve',
          'scope:report.export',
          'scope:report.view',
        }),
      );
    });
  });
}
