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/product/data/repositories/product_repository_remote.dart';

class _MockApiClient extends Mock implements ApiClient {}

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

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

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

  test('list는 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,
              'product_code': 'P-001',
              'product_name': '샘플',
              'vendor': {
                'id': 10,
                'vendor_code': 'V-010',
                'vendor_name': '테스트 벤더',
              },
              'uom': {'id': 5, 'uom_name': 'EA'},
            },
          ],
          'page': 1,
          'page_size': 20,
          'total': 1,
        },
        requestOptions: RequestOptions(path: '/api/v1/products'),
        statusCode: 200,
      ),
    );

    final result = await repository.list(
      page: 3,
      pageSize: 40,
      query: 'gear',
      vendorId: 10,
      uomId: 5,
      isActive: false,
    );

    expect(result.items, isNotEmpty);

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

    expect(query['include'], 'vendor,uom');
    expect(query['vendor_id'], 10);
    expect(query['uom_id'], 5);
    expect(query['active'], false);
    expect(query['page'], 3);
    expect(query['page_size'], 40);
    expect(query['q'], 'gear');
  });
}
