/**
 * Policy/safety document categories (Workstream 11). Two existing content areas
 * require staff acknowledgment: official/government **safety protocols** and
 * internal **handbook & policies**. Stored on `policy_documents.category`.
 */
export const POLICY_DOCUMENT_CATEGORIES = Object.freeze({
  SAFETY_PROTOCOL: 'safety_protocol',
  HANDBOOK_POLICY: 'handbook_policy',
});

export type PolicyDocumentCategory =
  (typeof POLICY_DOCUMENT_CATEGORIES)[keyof typeof POLICY_DOCUMENT_CATEGORIES];

export const POLICY_DOCUMENT_CATEGORY_VALUES: readonly PolicyDocumentCategory[] =
  Object.freeze(Object.values(POLICY_DOCUMENT_CATEGORIES));

export const POLICY_DOCUMENTS_QUERY_KEY = 'policy_documents';
export const POLICY_ACKNOWLEDGMENTS_QUERY_KEY = 'policy_acknowledgments';

/** Type guard for the stored `category` value. */
export function isPolicyDocumentCategory(
  value: unknown,
): value is PolicyDocumentCategory {
  return POLICY_DOCUMENT_CATEGORY_VALUES.some((category) => category === value);
}

/**
 * Domain rule (Workstream 11): editing a document's content bumps its version so
 * staff must re-acknowledge. An explicit `version` wins; otherwise the version
 * increments only when the content changed.
 */
export function nextPolicyDocumentVersion(
  currentVersion: number,
  contentChanged: boolean,
  explicitVersion?: number,
): number {
  if (explicitVersion !== undefined) {
    return explicitVersion;
  }
  return contentChanged ? currentVersion + 1 : currentVersion;
}
