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

import com.banesco.common.domain.model.Device;
import com.banesco.common.infrastructure.context.RequestContext;
import com.banesco.module.instruction.domain.model.Instruction;
import com.banesco.module.payment_instruction_request.domain.model.PaymentArrangement;
import com.banesco.module.payment_instruction_request.domain.model.PaymentInstructionTransactionInstanceRecord;
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.List;

@Getter
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@RegisterForReflection
public class PaymentInstructionRequestRequest {
    @NonNull
    private String customerReferenceFintechId;
    @NonNull
    private String appId;
    @NonNull
    private PaymentInstructionTransactionInstanceRecord paymentInstructionTransaction;
    @NonNull
    private Instruction procedureRequest;
    @NonNull
    private Device device;

    @JsonIgnore
    public String getRecipientId() {
        return paymentInstructionTransaction
            .getPaymentInstruction()
            .getPaymentArrangement()
            .getRecipientId();
    }

    @JsonIgnore
    public String getRecipientPhone() {
        return paymentInstructionTransaction
            .getPaymentInstruction()
            .getPaymentArrangement()
            .getRecipientPhone();
    }

    @JsonIgnore
    public String getBankCode() {
        return paymentInstructionTransaction
            .getPaymentInstruction()
            .getPaymentArrangement()
            .getBankingProduct()
            .getProductName();
    }

    @JsonIgnore
    public BigDecimal getAmount() {
        return paymentInstructionTransaction
            .getPaymentInstruction()
            .getPaymentArrangement()
            .getPaymentAmount();
    }

    @JsonIgnore
    public String getPurpose() {
        return paymentInstructionTransaction
            .getPaymentInstruction()
            .getPaymentArrangement()
            .getPaymentPurpose();
    }

    @JsonIgnore
    public String getEffectiveDate() {
        return paymentInstructionTransaction
            .getPaymentInstruction()
            .getPaymentArrangement()
            .getEffectiveDate();
    }

    @JsonIgnore
    public List<PaymentArrangement> getPaymentArrangement() {
        return paymentInstructionTransaction
            .getPaymentArrangement();
    }

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

    @JsonIgnore
    public static PaymentInstructionRequestRequest fromResource(
        String customerReferenceFintechId,
        String appId,
        PaymentInstructionRequestRequest request
    ) {
        return PaymentInstructionRequestRequest.builder()
            .customerReferenceFintechId(customerReferenceFintechId)
            .appId(appId)
            .paymentInstructionTransaction(request.getPaymentInstructionTransaction())
            .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();
    }
}