import 'package:dio/dio.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

import 'package:superport_v2/core/network/api_client.dart';
import 'package:superport_v2/features/masters/group/data/repositories/group_repository_remote.dart';

class _MockApiClient extends Mock implements ApiClient {}

void main() {
  late ApiClient apiClient;
  late GroupRepositoryRemote repository;

  setUpAll(() {
    registerFallbackValue(Options());
    registerFallbackValue(CancelToken());
  });

  setUp(() {
    apiClient = _MockApiClient();
    repository = GroupRepositoryRemote(apiClient: apiClient);
  });

  test('include 옵션이 쿼리 파라미터에 반영된다', () async {
    when(
      () => apiClient.get<Map<String, dynamic>>(
        any(),
        query: any(named: 'query'),
        options: any(named: 'options'),
        cancelToken: any(named: 'cancelToken'),
      ),
    ).thenAnswer(
      (_) async => Response<Map<String, dynamic>>(
        data: {
          'items': [
            {
              'id': 1,
              'group_name': '관리자',
              'is_default': true,
              'is_active': true,
            },
          ],
        },
        requestOptions: RequestOptions(path: '/api/v1/groups'),
        statusCode: 200,
      ),
    );

    await repository.list(includePermissions: true, includeEmployees: true);

    final captured = verify(
      () => apiClient.get<Map<String, dynamic>>(
        captureAny(),
        query: captureAny(named: 'query'),
        options: any(named: 'options'),
        cancelToken: any(named: 'cancelToken'),
      ),
    ).captured;
    final path = captured[0] as String;
    final query = captured[1] as Map<String, dynamic>;

    expect(path, equals('/api/v1/groups'));
    expect(query['include'], 'permissions,employees');
  });
}
