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/inventory/lookups/data/repositories/inventory_lookup_repository_remote.dart';

class _MockApiClient extends Mock implements ApiClient {}

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

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

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

  Future<void> stubGet(String path) async {
    when(
      () => apiClient.get<Map<String, dynamic>>(
        path,
        query: any(named: 'query'),
        options: any(named: 'options'),
        cancelToken: any(named: 'cancelToken'),
      ),
    ).thenAnswer(
      (_) async => Response<Map<String, dynamic>>(
        data: {
          'items': [
            {'id': 1, 'name': '샘플'},
          ],
        },
        requestOptions: RequestOptions(path: path),
        statusCode: 200,
      ),
    );
  }

  test('fetchTransactionStatuses는 /transaction-statuses를 호출한다', () async {
    const path = '/api/v1/transaction-statuses';
    await stubGet(path);

    final items = await repository.fetchTransactionStatuses();

    expect(items, isNotEmpty);
    final captured = verify(
      () => apiClient.get<Map<String, dynamic>>(
        captureAny(),
        query: captureAny(named: 'query'),
        options: any(named: 'options'),
        cancelToken: any(named: 'cancelToken'),
      ),
    ).captured;
    expect(captured[0], equals(path));
    final query = captured[1] as Map<String, dynamic>;
    expect(query['page'], 1);
    expect(query['page_size'], 200);
    expect(query['active'], true);
  });

  test('fetchApprovalActions는 is_active 파라미터 없이 호출한다', () async {
    const path = '/api/v1/approval-actions';
    await stubGet(path);

    await repository.fetchApprovalActions();

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