import 'package:superport_v2/core/common/models/paginated_result.dart';
import 'package:superport_v2/features/inventory/summary/domain/entities/inventory_detail.dart';
import 'package:superport_v2/features/inventory/summary/domain/entities/inventory_filters.dart';
import 'package:superport_v2/features/inventory/summary/domain/entities/inventory_product.dart';
import 'package:superport_v2/features/inventory/summary/domain/entities/inventory_summary.dart';
import 'package:superport_v2/features/inventory/summary/domain/entities/inventory_summary_list_result.dart';
import 'package:superport_v2/features/inventory/summary/domain/entities/inventory_vendor.dart';
import 'package:superport_v2/features/inventory/summary/domain/entities/inventory_warehouse.dart';
import 'package:superport_v2/features/inventory/summary/domain/entities/inventory_warehouse_balance.dart';
import 'package:superport_v2/features/inventory/summary/domain/repositories/inventory_repository.dart';

class FakeInventoryRepository implements InventoryRepository {
  InventorySummaryListResult? summaryResult;
  InventoryDetail? detailResult;
  Object? summaryError;
  Object? detailError;
  InventorySummaryFilter? lastSummaryFilter;
  InventoryDetailFilter? lastDetailFilter;

  @override
  Future<InventoryDetail> fetchDetail(
    int productId, {
    InventoryDetailFilter? filter,
  }) async {
    lastDetailFilter = filter;
    if (detailError != null) {
      throw detailError!;
    }
    return detailResult ?? buildDetail(productId);
  }

  @override
  Future<InventorySummaryListResult> listSummaries({
    InventorySummaryFilter? filter,
  }) async {
    lastSummaryFilter = filter;
    if (summaryError != null) {
      throw summaryError!;
    }
    return summaryResult ?? buildSummaryResult();
  }
}

InventorySummaryListResult buildSummaryResult() {
  final product = InventoryProduct(
    id: 1,
    code: 'P-1',
    name: '장비',
    vendor: const InventoryVendor(id: 9, name: '벤더'),
  );
  final refreshedAt = DateTime.utc(2025, 1, 1, 12);
  final summary = InventorySummary(
    product: product,
    totalQuantity: 10,
    warehouseBalances: [
      InventoryWarehouseBalance(
        warehouse: const InventoryWarehouse(id: 1, code: 'WH-1', name: '본사'),
        quantity: 10,
      ),
    ],
    recentEvent: null,
    updatedAt: DateTime.utc(2025, 1, 1),
    lastRefreshedAt: refreshedAt,
  );
  final paginated = PaginatedResult<InventorySummary>(
    items: [summary],
    page: 1,
    pageSize: 50,
    total: 1,
  );
  return InventorySummaryListResult(
    result: paginated,
    lastRefreshedAt: refreshedAt,
  );
}

InventoryDetail buildDetail(int productId) {
  final product = InventoryProduct(
    id: productId,
    code: 'P-$productId',
    name: '제품$productId',
    vendor: const InventoryVendor(id: 9, name: '벤더'),
  );
  return InventoryDetail(
    product: product,
    totalQuantity: 5,
    warehouseBalances: [
      InventoryWarehouseBalance(
        warehouse: const InventoryWarehouse(id: 1, code: 'WH-1', name: '본사'),
        quantity: 5,
      ),
    ],
    recentEvents: const [],
    updatedAt: DateTime.utc(2025, 1, 2),
    lastRefreshedAt: DateTime.utc(2025, 1, 2),
  );
}
