package domain.model;

import domain.dto.request.CustomerRequest;
import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.Data;

import java.util.Optional;

@Data
@RegisterForReflection
public class AccountClientContext {
    // Datos de entrada
    final long startExecution;
    final String requestId;
    final CustomerRequest request;
    final String appId, fintechId, finalCustomerId, redisKey;
    String username;

    Optional<ErrorCode> finalError = Optional.empty();

    public AccountClientContext(long startExecution, String requestId, CustomerRequest request, String... headers) {
        var customer = request.getCustomerPositionState().getCustomerReference();
        var sid = request.getDevice().getDeviceSessionReference();

        this.startExecution = startExecution;
        this.requestId = requestId;
        this.request = request;

        this.appId = headers[0];
        this.fintechId = headers[1];
        this.finalCustomerId = customer.getCustomerIdType().concat(customer.getCustomerId());
        
        this.username =  Optional.ofNullable(headers[2]).orElse(this.finalCustomerId);

        this.redisKey = Optional.ofNullable(sid)
                .filter(s -> !s.isBlank())
                .map(s -> this.finalCustomerId + sid)
                .orElse(this.finalCustomerId);
    }

}