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

import com.banesco.common.domain.exception.HttpStatusCodeException;
import com.banesco.common.domain.model.Identifier;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.*;

import java.util.Arrays;
import java.util.Objects;

@Getter
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@RegisterForReflection
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Instruction {
    private InstructionIdentification instructionIdentifier;     // Request JSON: "recipientId"
    private String instructionDescription;                       // Request JSON: "signature"
    private InstructionPurposeType instructionPurposeType;       // Request JSON: "channelOrigin" (BOLE)

    public static Instruction fromResource(
        String partyReferenceId,
        String channelCode,
        String signatureIdentifier
    ) {
        boolean isChannelCodeValid = (Arrays.stream(
            InstructionPurposeType.values()
        ).anyMatch(type -> Objects.equals(type.name(), channelCode)));

        if(!isChannelCodeValid) {
            throw HttpStatusCodeException.badRequest("VDE02", "channelCode");
        }

        return Instruction.builder()
                .instructionIdentifier(
                    InstructionIdentification.builder()
                        .identification(
                            Identifier.builder()
                                .identifierValue(partyReferenceId)
                                .build()
                        )
                        .identificationType(InstructionIdentificationType.INSTRUCTION_NUMBER)
                        .build()
                )
                .instructionPurposeType(InstructionPurposeType.valueOf(channelCode))
                .instructionDescription(signatureIdentifier)
                .build();
    }
}
