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/presentation/controllers/inventory_summary_controller.dart';

import 'fake_inventory_repository.dart';

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

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

    test('fetch 저장 시 결과와 페이징 상태를 갱신한다', () async {
      repository.summaryResult = buildSummaryResult();

      await controller.fetch();

      expect(controller.result, isNotNull);
      expect(controller.result!.items, isNotEmpty);
      expect(controller.isLoading, isFalse);
      expect(controller.errorMessage, isNull);
      expect(repository.lastSummaryFilter?.page, 1);
      expect(controller.lastRefreshedAt, DateTime.utc(2025, 1, 1, 12));
    });

    test('쿼리/정렬/필터 업데이트가 상태에 반영된다', () {
      controller
        ..updateQuery('   camera   ')
        ..updateProductName('렌즈')
        ..updateVendorName('슈퍼')
        ..updateWarehouse(7)
        ..toggleIncludeEmpty(true)
        ..updateSort('total_quantity', order: 'asc')
        ..updatePageSize(30);

      expect(controller.query, 'camera');
      expect(controller.productName, '렌즈');
      expect(controller.vendorName, '슈퍼');
      expect(controller.warehouseId, 7);
      expect(controller.includeEmpty, isTrue);
      expect(controller.sort, 'total_quantity');
      expect(controller.order, 'asc');
      expect(controller.pageSize, 30);
    });

    test('요청 실패 시 오류 메시지를 저장한다', () async {
      repository.summaryError = Exception('boom');

      await controller.fetch();

      expect(controller.errorMessage, contains('boom'));
      expect(controller.isLoading, isFalse);
    });
  });
}
