package com.banesco.module.payment_initiation_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_initiation_request.domain.model.PaymentInitiationTransactionInstanceRecord;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.netty.util.internal.StringUtil;
import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.*;

import java.math.BigDecimal;

@Getter
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@RegisterForReflection
public class PaymentInitiationRequestRequest {
    @NonNull
    private String customerReferenceFintechId;
    @NonNull
    private String appId;
    @NonNull
    private PaymentInitiationTransactionInstanceRecord paymentInitiationTransaction;
    @NonNull
    private Instruction procedureRequest;
    @NonNull
    private Device device;

    @JsonIgnore
    public String getPaymentTransactionType() {
        return paymentInitiationTransaction
            .getPaymentTransactionType()
            .name();
    }

    @JsonIgnore
    public String getPayerId() {
        return paymentInitiationTransaction
            .getPayerReference()
            .getPartyIdentification()
            .get(0)
            .getPartyIdentification()
            .getIdentifierValue();
    }

    @JsonIgnore
    public String getPayeeId() {
        return paymentInitiationTransaction
            .getPayeeReference()
            .getPartyIdentification()
            .get(0)
            .getPartyIdentification()
            .getIdentifierValue();
    }

    @JsonIgnore
    public BigDecimal getAmount() {
        return paymentInitiationTransaction
            .getAmount();
    }

    @JsonIgnore
    public String getCurrency() {
        return paymentInitiationTransaction
            .getCurrencyCode();
    }

    @JsonIgnore
    public String getPurpose() {
        return paymentInitiationTransaction
            .getPaymentPurpose();
    }

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

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