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

import com.banesco.common.domain.model.Identifier;
import com.banesco.module.party.domain.model.Party;
import com.banesco.module.party.domain.model.PartyIdentification;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.*;

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

@Getter
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@RegisterForReflection
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Instruction {
    private InstructionInvolvement instructionInvolvement;       // Request JSON: "recipientId"
    private List<InstructionDateTime> instructionDate;           // Request JSON: "dateCreate"
    private InstructionIdentification instructionIdentifier;     // Request JSON: "id"
    private String instructionPurposeType;                       // Request JSON: "channelOrigin" (BOLE)

    public static Instruction fromResource(
        String partyReferenceId,
        String initiatedDate,
        String instructionRequestId,
        String channelCode
    ) {
        return Instruction.builder()
                .instructionInvolvement(
                    InstructionInvolvement.builder()
                        .partyReference(
                            Party.builder()
                                .partyIdentification(List.of(
                                    PartyIdentification.builder()
                                        .partyIdentification(
                                            Identifier.builder()
                                                .identifierValue(Objects.toString(partyReferenceId, ""))
                                                .build()
                                        )
                                        .build()
                                ))
                                .build()
                        )
                        .build()
                )
                .instructionIdentifier(
                    InstructionIdentification.builder()
                        .identification(
                            Identifier.builder()
                                .identifierValue(Objects.toString(instructionRequestId, ""))
                                .build()
                        )
                        .identificationType(InstructionIdentificationType.INSTRUCTION_NUMBER)
                        .build()
                )
                .instructionDate(List.of(
                    InstructionDateTime.builder()
                        .date(Objects.toString(initiatedDate, ""))
                        .build()
                ))
                .instructionPurposeType(channelCode)
                .build();
    }
}
