package com.banesco.module.msg_request_header.domain.model;

import com.banesco.common.domain.model.Device;
import com.banesco.common.infrastructure.context.RequestContext;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

@Getter
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@RegisterForReflection
public class MsgRqHdr {
    @JsonProperty("MessageDate")
    private String MessageDate;
    @JsonProperty("MessageTime")
    private String MessageTime;
    @JsonProperty("RequestId")
    private String RequestId;
    @JsonProperty("LastStatusRequest")
    private String LastStatusRequest;
    @JsonProperty("FinalStatusRequest")
    private String FinalStatusRequest;
    @JsonProperty("ResumeId")
    private String ResumeId;
    @JsonProperty("ReverseId")
    private String ReverseId;
    @JsonProperty("LineId")
    private String LineId;
    @JsonProperty("SourceChannelCode")
    private String SourceChannelCode;
    @JsonProperty("SupervisorCode")
    private String SupervisorCode;
    @JsonProperty("OperatorCode")
    private String OperatorCode;
    @JsonProperty("RequestedOperationType")
    private String RequestedOperationType;
    @JsonProperty("NetworkTrnInfo")
    private NetworkTrnInfo NetworkTrnInfo;
    @JsonProperty("ApplicantData")
    private ApplicantData ApplicantData;
    @JsonProperty("RecCtrlIn")
    private RecCtrlIn RecCtrlIn;
    @JsonProperty("ReturnValue")
    private String ReturnValue;
    @JsonProperty("MessageId")
    private String MessageId;
    @JsonProperty("Priority")
    private String Priority;
    @JsonProperty("VBProtocol")
    private VbProtocol VBProtocol;

    private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @JsonIgnore
    public MsgRqHdr withOptions(Device device) {
        if(Objects.isNull(this.getApplicantData())) {
            this.ApplicantData = com.banesco.module.msg_request_header.domain.model.ApplicantData.builder()
                .IpAddress(device.getDeviceIp())
                .build();
        } else {
            this.ApplicantData = this.ApplicantData.withOptions(device);
        }

        String[] dateTime = LocalDateTime.now().format(dateTimeFormatter).split(" ");
        String date = dateTime[0];
        String time = dateTime[1];

        this.RequestId = RequestContext.getRequestId();
        this.MessageDate = date;
        this.MessageTime = time;

        this.NetworkTrnInfo = this.NetworkTrnInfo.withOptions(date, time);

        return this;
    }
}