import 'package:pshared/data/dto/payment/payment.dart';
import 'package:pshared/data/mapper/payment/operation.dart';
import 'package:pshared/data/mapper/payment/quote.dart';
import 'package:pshared/data/mapper/payment/response_endpoint.dart';
import 'package:pshared/models/payment/payment.dart';
import 'package:pshared/models/payment/state.dart';


extension PaymentDTOMapper on PaymentDTO {
  Payment toDomain() => Payment(
    paymentRef: paymentRef,
    state: state,
    comment: comment,
    source: source?.toDomain(),
    destination: destination?.toDomain(),
    orchestrationState: paymentOrchestrationStateFromValue(state),
    failureCode: failureCode,
    failureReason: failureReason,
    operations: operations.map((item) => item.toDomain()).toList(),
    lastQuote: lastQuote?.toDomain(),
    metadata: metadata,
    createdAt: createdAt == null ? null : DateTime.tryParse(createdAt!),
  );
}

extension PaymentMapper on Payment {
  PaymentDTO toDTO() => PaymentDTO(
    paymentRef: paymentRef,
    state: state ?? paymentOrchestrationStateToValue(orchestrationState),
    comment: comment,
    source: source?.toDTO(),
    destination: destination?.toDTO(),
    failureCode: failureCode,
    failureReason: failureReason,
    operations: operations.map((item) => item.toDTO()).toList(),
    lastQuote: lastQuote?.toDTO(),
    metadata: metadata,
    createdAt: createdAt?.toUtc().toIso8601String(),
  );
}
