import 'package:dio/dio.dart';
import 'package:superport_v2/core/network/api_client.dart';
import 'package:superport_v2/core/network/api_routes.dart';

import '../../domain/entities/inventory_detail.dart';
import '../../domain/entities/inventory_filters.dart';
import '../../domain/entities/inventory_summary_list_result.dart';
import '../../domain/repositories/inventory_repository.dart';
import '../dtos/inventory_detail_response.dart';
import '../dtos/inventory_summary_response.dart';

/// 재고 현황 API를 호출하는 원격 저장소 구현체.
class InventoryRepositoryRemote implements InventoryRepository {
  InventoryRepositoryRemote({required ApiClient apiClient}) : _api = apiClient;

  final ApiClient _api;

  static const _summaryPath = ApiRoutes.inventorySummary;

  @override
  Future<InventorySummaryListResult> listSummaries({
    InventorySummaryFilter? filter,
  }) async {
    final effectiveFilter = filter ?? const InventorySummaryFilter();
    final response = await _api.get<Map<String, dynamic>>(
      _summaryPath,
      query: effectiveFilter.toQuery(),
      options: Options(responseType: ResponseType.json),
    );
    final body = response.data ?? const <String, dynamic>{};
    return InventorySummaryResponse.fromJson(body).toEntity();
  }

  @override
  Future<InventoryDetail> fetchDetail(
    int productId, {
    InventoryDetailFilter? filter,
  }) async {
    final effectiveFilter = filter ?? const InventoryDetailFilter();
    final response = await _api.get<Map<String, dynamic>>(
      ApiRoutes.inventorySummaryDetail(productId),
      query: effectiveFilter.toQuery(),
      options: Options(responseType: ResponseType.json),
    );
    final body = response.data ?? const <String, dynamic>{};
    return InventoryDetailResponse.fromJson(body).toEntity();
  }
}
