import 'package:flutter_test/flutter_test.dart';
import 'package:superport_v2/features/inventory/summary/application/inventory_service.dart';
import 'package:superport_v2/features/inventory/summary/domain/entities/inventory_filters.dart';
import 'package:superport_v2/features/inventory/summary/presentation/controllers/inventory_detail_controller.dart';

import 'fake_inventory_repository.dart';

void main() {
  group('InventoryDetailController', () {
    late FakeInventoryRepository repository;
    late InventoryDetailController controller;

    setUp(() {
      repository = FakeInventoryRepository();
      controller = InventoryDetailController(
        service: InventoryService(repository: repository),
      );
    });

    test('fetch는 상세 정보를 로드하고 캐시한다', () async {
      await controller.fetch(1);

      expect(controller.detailOf(1), isNotNull);
      expect(controller.isLoading(1), isFalse);
      expect(controller.errorOf(1), isNull);
      expect(repository.lastDetailFilter, isNotNull);
    });

    test('동일 필터로 재요청 시 추가 호출을 건너뛴다', () async {
      await controller.fetch(2);
      repository.detailError = Exception('should not be thrown');

      await controller.fetch(2);

      expect(controller.errorOf(2), isNull);
    });

    test('필터 변경 시 강제로 다시 조회한다', () async {
      await controller.fetch(3);
      repository.detailError = Exception('boom');

      await controller.updateEventLimit(3, 50);

      expect(controller.errorOf(3), contains('boom'));
      expect(repository.lastDetailFilter?.eventLimit, 50);
    });

    test('오류를 명시적으로 초기화할 수 있다', () async {
      repository.detailError = Exception('boom');
      await controller.fetch(5, filter: const InventoryDetailFilter());

      expect(controller.errorOf(5), isNotNull);

      controller.clearError(5);

      expect(controller.errorOf(5), isNull);
    });
  });
}
