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

import java.util.Map;
import java.util.Objects;

import static java.util.Map.entry;

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

    @JsonIgnore
    public String getId() {
        return procedureRequest
            .getInstructionIdentifier()
            .getIdentification()
            .getIdentifierValue();
    }

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

    @JsonIgnore
    public Map<String, String> toParams() {
        return Map.ofEntries(
            entry("paymentStatusId", Objects.toString(getId(), "")),
            entry("channelCode", Objects.toString(getChannelCode(), ""))
        );
    }

    @JsonIgnore
    public Map<String, String> toQueryString() {
        return Map.ofEntries(
            entry("appId", Objects.toString(getAppId(), "")),
            entry("customerReferenceFintechId", Objects.toString(getCustomerReferenceFintechId(), "")),
            entry("deviceType", Objects.toString(getDevice().getDeviceType(), "")),
            entry("deviceDescription", Objects.toString(getDevice().getDeviceDescription(), "")),
            entry("deviceIp", Objects.toString(getDevice().getDeviceIp(), "")),
            entry("deviceSessionReference", (!StringUtil.isNullOrEmpty(getDevice().getDeviceSessionReference()))
                ? getDevice().getDeviceSessionReference()
                : RequestContext.getRequestId()
            )
        );
    }
}