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

import com.banesco.common.domain.model.Device;
import com.banesco.common.infrastructure.context.RequestContext;
import com.banesco.module.service_order_status_control.domain.model.ServicingOrderProcedureInstanceRecord;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.netty.util.internal.StringUtil;
import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.*;

@Getter
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@RegisterForReflection
public class ServiceOrderStatusControlRequest {
    @NonNull
    private String customerReferenceFintechId;
    @NonNull
    private String appId;
    @NonNull
    private ServicingOrderProcedureInstanceRecord servicingOrderProcedureInstanceRecord;
    @NonNull
    private Device device;

    @JsonIgnore
    public String getBankId() {
        return servicingOrderProcedureInstanceRecord
            .getWorkProduct()
            .getProductIdentification()
            .getIdentifierValue();
    }

    @JsonIgnore
    public String getServiceCode() {
        return servicingOrderProcedureInstanceRecord
            .getServiceOrderDescription();
    }

    @JsonIgnore
    public String getServiceType() {
        return servicingOrderProcedureInstanceRecord
            .getServiceOrderType();
    }

    @JsonIgnore
    public String getServiceDescription() {
        String accountId = servicingOrderProcedureInstanceRecord
            .getWorkProduct()
            .getProductReference();
        String customerId = servicingOrderProcedureInstanceRecord
            .getCustomerReference()
            .getPartyIdentification()
            .get(0)
            .getPartyIdentification()
            .getIdentifierValue();

        if(!StringUtil.isNullOrEmpty(accountId)) {
            customerId = customerId + accountId;
        }

        return customerId;
    }

    @JsonIgnore
    public String getServiceObserver() {
        return servicingOrderProcedureInstanceRecord
            .getServicingOrderTask()
            .getTask();
    }

    @JsonIgnore
    public String getClientId() {
        return servicingOrderProcedureInstanceRecord
            .getCustomerReference()
            .getPartyIdentification()
            .get(0)
            .getPartyIdentification()
            .getIdentifierValue();
    }

    @JsonIgnore
    public String getAccountNumber() {
        return servicingOrderProcedureInstanceRecord
            .getWorkProduct()
            .getProductReference();
    }

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