import { geliosProviderPackageV1 } from "../v1/package.mjs";

export const GELIOS_PROVIDER_PACKAGE_ID = "gelios.provider.v2";
export const GELIOS_PROVIDER_PACKAGE_VERSION = "2.0.0";
export const GELIOS_POSITIONS_DATA_PRODUCT_ID = "fleet.positions.current.v1";
export const GELIOS_POSITIONS_DATA_PRODUCT_VERSION = "1.0.0";
export const GELIOS_POSITIONS_ONTOLOGY_REVISION = "ontology.map.moving_object.v1";
export const GELIOS_UNIT_SOURCE_ID_PREFIX = "gelios-unit-";

const AUTH_MODE_ID = "gelios.sdk-query-token.v2";
const CAPABILITY_ID = "gelios.units.current.sdk.read";
const REALTIME_PROFILE_ID = "gelios.positions.current.realtime.v2";
const MANUAL_PROFILE_ID = "gelios.positions.current.manual.v2";
const MAPPING_ID = "gelios.units.to.fleet.positions.current.v2";
const TEMPLATE_ID = "gelios.positions.current.l2.v2";

const value = structuredClone(geliosProviderPackageV1);
value.id = GELIOS_PROVIDER_PACKAGE_ID;
value.version = GELIOS_PROVIDER_PACKAGE_VERSION;
value.manifest = {
  ...value.manifest,
  id: "gelios.provider.manifest.v2",
  version: GELIOS_PROVIDER_PACKAGE_VERSION,
  authModeIds: [AUTH_MODE_ID],
  capabilityIds: [CAPABILITY_ID],
  collectionProfileIds: [REALTIME_PROFILE_ID, MANUAL_PROFILE_ID],
  mappingContractIds: [MAPPING_ID],
  l2TemplateIds: [TEMPLATE_ID],
};
value.authModes = [{
  id: AUTH_MODE_ID,
  kind: "api_token",
  credentialOwner: "ndc_l2_credentials",
  bindingCardinality: "one_per_connection",
  transport: {
    placement: "query",
    name: "token",
  },
  tokenLifecycle: {
    artifacts: ["access"],
    requestArtifact: "access",
    refreshMode: "not_applicable",
  },
  secretHandling: {
    store: "ndc_l2_credentials",
    exposeToGraph: false,
    persistInPackage: false,
  },
}];
value.capabilities = [{
  ...value.capabilities[0],
  id: CAPABILITY_ID,
  authModeId: AUTH_MODE_ID,
  request: {
    method: "GET",
    baseUrl: "https://admin.geliospro.com",
    path: "/sdk/",
    query: {
      svc: "get_units",
      params: "{}",
    },
    response: value.capabilities[0].request.response,
  },
}];
value.collectionProfiles = value.collectionProfiles.map((profile, index) => ({
  ...profile,
  id: index === 0 ? REALTIME_PROFILE_ID : MANUAL_PROFILE_ID,
  version: GELIOS_PROVIDER_PACKAGE_VERSION,
  capabilityIds: [CAPABILITY_ID],
  mappingContractId: MAPPING_ID,
  l2TemplateId: TEMPLATE_ID,
}));
value.mappingContracts = [{
  ...value.mappingContracts[0],
  id: MAPPING_ID,
  version: GELIOS_PROVIDER_PACKAGE_VERSION,
  sourceCapabilityId: CAPABILITY_ID,
}];
value.l2Templates = [{
  ...value.l2Templates[0],
  id: TEMPLATE_ID,
  version: GELIOS_PROVIDER_PACKAGE_VERSION,
  credentialBindings: value.l2Templates[0].credentialBindings.map((binding) => (
    binding.role === "provider" ? { ...binding, authModeId: AUTH_MODE_ID } : binding
  )),
  steps: value.l2Templates[0].steps.map((step) => {
    if (step.kind === "provider_request" || step.kind === "extract_items") {
      return { ...step, capabilityId: CAPABILITY_ID };
    }
    if (step.kind === "semantic_mapping") return { ...step, mappingContractId: MAPPING_ID };
    return step;
  }),
}];

export const geliosProviderPackageV2 = deepFreeze(value);

function deepFreeze(input) {
  if (!input || typeof input !== "object" || Object.isFrozen(input)) return input;
  Object.freeze(input);
  for (const child of Object.values(input)) deepFreeze(child);
  return input;
}
