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

import '../../domain/entities/stock_transaction_input.dart';
import '../../domain/repositories/stock_transaction_repository.dart';

/// 재고 트랜잭션 라인 API를 호출하는 원격 저장소 구현체.
class TransactionLineRepositoryRemote implements TransactionLineRepository {
  TransactionLineRepositoryRemote({required ApiClient apiClient})
    : _api = apiClient;

  final ApiClient _api;

  static const _basePath = '${ApiRoutes.apiV1}/stock-transactions';
  static const _linePath = '${ApiRoutes.apiV1}/transaction-lines';

  @override
  Future<void> addLines(
    int transactionId,
    List<TransactionLineCreateInput> lines,
  ) async {
    await _api.post<void>(
      '$_basePath/$transactionId/lines',
      data: {
        'id': transactionId,
        'lines': lines.map((line) => line.toJson()).toList(growable: false),
      },
    );
  }

  @override
  Future<void> updateLines(
    int transactionId,
    List<TransactionLineUpdateInput> lines,
  ) async {
    await _api.patch<void>(
      '$_basePath/$transactionId/lines',
      data: {
        'id': transactionId,
        'lines': lines.map((line) => line.toJson()).toList(growable: false),
      },
    );
  }

  @override
  Future<void> deleteLine(int lineId) async {
    await _api.delete<void>('$_linePath/$lineId');
  }

  @override
  Future<void> restoreLine(int lineId) async {
    await _api.post<void>('$_linePath/$lineId/restore');
  }
}
