/**
 * Server-only: compute a deterministic SHA-256 hash for an OrganizationModel snapshot.
 *
 * Uses node:crypto — never import this file from browser-reachable code.
 * For the browser-safe canonical serializer, use `canonicalStringifyOrganizationModel`
 * from `@repo/core/organization-model`.
 */

import { createHash } from 'node:crypto'
import type { OrganizationModel } from '../types'
import { canonicalStringifyOrganizationModel } from '../snapshot-hash'

/**
 * Compute a deterministic SHA-256 hex hash for an OrganizationModel.
 *
 * Delegates serialization to `canonicalStringifyOrganizationModel` (key-sorted,
 * excludes `snapshotHash` and `domainMetadata[*].lastModified`) then hashes with SHA-256.
 *
 * @returns 64-character lowercase hex string
 */
export function computeOrganizationModelSnapshotHash(model: OrganizationModel): string {
  return createHash('sha256').update(canonicalStringifyOrganizationModel(model)).digest('hex')
}
