package com.banesco.module.service_order_status_control.infrastructure.mapper;

import com.banesco.common.domain.model.Event;
import com.banesco.common.domain.model.Identifier;
import com.banesco.module.instruction.domain.model.Instruction;
import com.banesco.module.instruction.domain.model.InstructionIdentification;
import com.banesco.module.service_order_status.domain.dto.response.ServiceOrderStatusResponse;
import com.banesco.module.service_order_status_control.domain.dto.request.ServiceOrderStatusControlRequest;
import com.banesco.module.service_order_status_control.domain.dto.response.ServiceOrderStatusControlResponse;
import com.banesco.module.service_order_status_control.domain.model.ServiceOrderWorkTaskResult;
import com.banesco.module.transaction.domain.model.Transaction;

import java.util.List;
import java.util.Objects;

public class ServicingIssueMapper {

    private ServicingIssueMapper() {}

    public static ServiceOrderStatusControlResponse toModel(
        ServiceOrderStatusControlRequest request,
        ServiceOrderStatusResponse response
    ) {
        return ServiceOrderStatusControlResponse.builder()
            .servicingOrderWorkTaskResult(buildWorkTaskResult(request, response))
            .transaction(buildTransaction(response))
            .build();
    }

    private static ServiceOrderWorkTaskResult buildWorkTaskResult(
        ServiceOrderStatusControlRequest request,
        ServiceOrderStatusResponse response
    ) {
        String task = request.getServiceObserver();

        if(
            !Objects.isNull(response.getCard()) &&
            !response.getCard().isEmpty() &&
            !Objects.isNull(response.getCard().get(0).getCardStatus()) &&
            !response.getCard().get(0).getCardStatus().isEmpty()
        ) {
            task = response.getCard().get(0).getCardStatus().get(0).getStatusDesc();
        }

        return ServiceOrderWorkTaskResult.builder()
            .task(task)
            .build();
    }

    private static Transaction buildTransaction(
        ServiceOrderStatusResponse response
    ) {
        if(Objects.isNull(response.getMsgRsHdr())) {
            return null;
        }

        return Transaction.builder()
            .transactionIdentification(
                Identifier.builder()
                    .identifierValue(response.getMsgRsHdr().getTransactionCode())
                    .build()
            )
            .transactionType(response.getMsgRsHdr().getTransactionType())
            .transactionConsecutive(response.getMsgRsHdr().getTransactionConsecutive())
            .transactionDescription(response.getMsgRsHdr().getDetailReg())
            .event(
                Event.builder()
                    .eventIdentification(
                        Identifier.builder()
                            .identifierValue(response.getMsgRsHdr().getRequestId())
                            .build()
                    )
                    .eventSource(response.getMsgRsHdr().getAppName())
                    .eventType(response.getMsgRsHdr().getOperationType())
                    .eventStatus(response.getMsgRsHdr().getFinalStatusRequest())
                    .eventDescription(response.getMsgRsHdr().getCountDataOut())
                    .build()
            )
            .instruction(List.of(
                Instruction.builder()
                    .instructionIdentifier(
                        InstructionIdentification.builder()
                            .instructionIdentification(
                                Identifier.builder()
                                    .identifierValue(response.getMsgRsHdr().getSupervisorCode())
                                    .build()
                            )
                            .build()
                    )
                    .instructionDescription((!Objects.isNull(response.getMsgRsHdr().getRegisterNumber()))
                        ? String.join(", ", response.getMsgRsHdr().getRegisterNumber())
                        : null
                    )
                    .build()
            ))
            .build();
    }
}