package com.banesco.module.correspondence.domain.dto.request;

import com.banesco.common.domain.model.Device;
import com.banesco.common.domain.model.PaymentInstruction;
import com.banesco.common.infrastructure.context.RequestContext;
import com.banesco.module.correspondence.domain.model.CorrespondenceOutbound;
import com.banesco.module.instruction.domain.model.Instruction;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.netty.util.internal.StringUtil;
import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.*;

import java.math.BigDecimal;
import java.util.Objects;

@Getter
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@RegisterForReflection
public class CorrespondenceRequest {
    @NonNull
    private String customerReferenceFintechId;
    @NonNull
    private String appId;
    @NonNull
    private CorrespondenceOutbound correspondenceOutbound;
    private PaymentInstruction paymentInstruction;
    @NonNull
    private Instruction procedureRequest;
    @NonNull
    private Device device;

    @JsonIgnore
    public String getCodeType() {
        return correspondenceOutbound
            .getSourceReference();
    }

    @JsonIgnore
    public String getRecipientId() {
        return correspondenceOutbound
            .getCustomerReference()
            .getPartyIdentification()
            .get(0)
            .getPartyIdentification()
            .getIdentifierValue();
    }

    @JsonIgnore
    public BigDecimal getAmount() {
        if(
            Objects.isNull(paymentInstruction) ||
            Objects.isNull(paymentInstruction.getAmount()) ||
            paymentInstruction.getAmount().isEmpty()
        ) {
            return null;
        }

        return paymentInstruction
            .getAmount()
            .get(0)
            .getValue();
    }

    @JsonIgnore
    public String getCurrency() {
        if(
            Objects.isNull(paymentInstruction) ||
            Objects.isNull(paymentInstruction.getAmount()) ||
            paymentInstruction.getAmount().isEmpty()
        ) {
            return null;
        }

        return paymentInstruction
            .getAmount()
            .get(0)
            .getCurrencyCode();
    }

    @JsonIgnore
    public String getExpirationDate() {
        return correspondenceOutbound
            .getDate();
    }

    @JsonIgnore
    public Long getExpirationTime() {
        return correspondenceOutbound
            .getTime();
    }

    @JsonIgnore
    public String getChannelCode() {
        return procedureRequest
            .getInstructionPurposeType();
    }

    @JsonIgnore
    public static CorrespondenceRequest fromResource(
        String customerReferenceFintechId,
        String appId,
        CorrespondenceRequest request
    ) {
        return CorrespondenceRequest.builder()
            .customerReferenceFintechId(customerReferenceFintechId)
            .appId(appId)
            .correspondenceOutbound(request.getCorrespondenceOutbound())
            .paymentInstruction(request.getPaymentInstruction())
            .procedureRequest(request.getProcedureRequest())
            .device(
                Device.builder()
                    .deviceType(request.getDevice().getDeviceType())
                    .deviceDescription(request.getDevice().getDeviceDescription())
                    .deviceIp(request.getDevice().getDeviceIp())
                    .deviceSessionReference(
                        (!StringUtil.isNullOrEmpty(request.getDevice().getDeviceSessionReference()))
                            ? request.getDevice().getDeviceSessionReference()
                            : RequestContext.getRequestId()
                    )
                    .build()
            )
            .build();
    }
}