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

import com.banesco.common.domain.model.Device;
import com.banesco.common.infrastructure.context.RequestContext;
import com.banesco.module.document.domain.model.Document;
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.Objects;

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

    @JsonIgnore
    public String getDocumentName() {
        return document.getDocumentName();
    }

    @JsonIgnore
    public String getDocumentDirectory() {
        return document
            .getDocumentLocation()
            .get(0)
            .getLocationReference()
            .getLocationValue();
    }

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

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