package com.banesco.module.correspondence.infrastructure.mapper;

import com.banesco.common.domain.model.Identifier;
import com.banesco.module.security_notification.domain.dto.request.SecurityNotificationRequest;
import com.banesco.module.security_notification.domain.model.Operation;
import com.banesco.module.security_notification.domain.model.SecurityNotification;
import com.banesco.module.correspondence.domain.dto.request.CorrespondenceRequest;
import com.banesco.module.correspondence.domain.model.CorrespondenceTransaction;
import com.banesco.module.transaction.domain.model.*;

import java.util.List;

public class ServicingIssueMapper {

    private ServicingIssueMapper() {}

    public static SecurityNotificationRequest toSecurityNotification(
        CorrespondenceRequest request
    ) {
        return SecurityNotificationRequest.builder()
            .fintechId(request.getCustomerReferenceFintechId())
            .operation(
                Operation.builder()
                    .codeAlert(request.getCodeType())
                    .recipientIdentification(request.getRecipientId())
                    .amount(request.getAmount())
                    .currency(request.getCurrency())
                    .expirationDate(request.getExpirationDate())
                    .expirationTime(request.getExpirationTime())
                    .build()
            )
            .channelOrigin(request.getChannelCode())
            .build();
    }

    public static CorrespondenceTransaction toModel(SecurityNotification response) {
        return CorrespondenceTransaction.builder()
            .transaction(buildTransaction(response))
            .build();
    }

    private static Transaction buildTransaction(SecurityNotification response) {
        return Transaction.builder()
            .transactionIdentification(
                Identifier.builder()
                    .identifierValue(String.valueOf(response.getId()))
                    .build()
            )
            .transactionDate(buildTransactionDateTimeList(response))
            .build();
    }

    private static List<TransactionDateTime> buildTransactionDateTimeList(SecurityNotification response) {
        return List.of(
            TransactionDateTime.builder()
                .dateType(TransactionDateTimeType.INITIATED_DATE)
                .date(response.getDateCreate())
                .build()
        );
    }
}