package com.cloudhandson.vpdbackoffice.domain.vpd;

import java.util.List;

public record VpdPolicyFormOptions(
    List<String> policyNames,
    List<String> schemaOwners,
    List<String> owners,
    List<VpdFunctionOption> functions,
    List<VpdPolicyTemplateOption> policyTemplates,
    List<String> statementTypes
) {

  public String defaultPermissionFunctionKey() {
    return functions.stream()
        .filter(VpdFunctionOption::permissionSystemDefault)
        .findFirst()
        .map(VpdFunctionOption::value)
        .orElse("");
  }

  public String defaultPermissionFunctionKey(String objectName) {
    String preferredFunction = objectName != null
        && objectName.toUpperCase(java.util.Locale.ROOT).startsWith("HMM_LEAVE_")
            ? "HMM_LEAVE_VPD_FILTER"
            : "CB_AGENT_DOC_VPD_FILTER";
    return functions.stream()
        .filter(function -> preferredFunction.equalsIgnoreCase(function.functionName()))
        .findFirst()
        .map(VpdFunctionOption::value)
        .orElseGet(this::defaultPermissionFunctionKey);
  }
}
