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 TransactionCustomerRepositoryRemote
    implements TransactionCustomerRepository {
  TransactionCustomerRepositoryRemote({required ApiClient apiClient})
    : _api = apiClient;

  final ApiClient _api;

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

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

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

  @override
  Future<void> deleteCustomer(int customerLinkId) async {
    await _api.delete<void>('$_customerPath/$customerLinkId');
  }
}
