import { appendCinemaReviewDecision, type CinemaReviewAppendResult } from './cinema-review-store.js'; import type { CinemaReviewAuthorityVerifier, CinemaReviewCommand, CinemaReviewDecision, CinemaReviewRole, } from './cinema-review-types.js'; import { cinemaReviewDecisionHash } from './cinema-review-validation.js'; const ALLOWED_ROLES: Record = { note: ['reviewer', 'editor', 'director', 'producer'], pass: ['reviewer', 'editor', 'director', 'producer'], reject: ['reviewer', 'editor', 'director', 'producer'], redesign: ['editor', 'director', 'producer'], }; export async function executeCinemaReviewCommand(input: { root: string; command: CinemaReviewCommand; verifyAuthority: CinemaReviewAuthorityVerifier; executedAt?: string; }): Promise { const { command } = input; if (!command.authorizationToken.trim()) throw new Error('Cinema live review requires an authorization token'); const executedAt = input.executedAt ?? new Date().toISOString(); if (Number.isNaN(Date.parse(executedAt))) throw new Error('Cinema review execution time must be an ISO timestamp'); const authority = await input.verifyAuthority({ projectSlug: command.projectSlug, attemptId: command.attemptId, kind: command.kind, authorizationToken: command.authorizationToken, }); if (authority.projectSlug !== command.projectSlug) throw new Error('Cinema review authority is scoped to a different project'); if (!authority.actorId.trim() || !authority.evidenceId.trim()) throw new Error('Cinema review authority requires actor and evidence identity'); if (Number.isNaN(Date.parse(authority.verifiedAt)) || Number.isNaN(Date.parse(authority.validUntil))) throw new Error('Cinema review authority timestamps must be valid'); if (Date.parse(authority.verifiedAt) > Date.parse(executedAt)) throw new Error('Cinema review authority verification is in the future'); if (Date.parse(authority.validUntil) <= Date.parse(executedAt)) throw new Error('Cinema review authority has expired'); if (!authority.roles.some((role) => ALLOWED_ROLES[command.kind].includes(role))) throw new Error(`Cinema review authority cannot perform ${command.kind}`); const evidenceIds = [...new Set([...command.evidenceIds, authority.evidenceId])]; const decisionInput: Omit = { decisionId: command.decisionId, attemptId: command.attemptId, shotId: command.shotId, kind: command.kind, reviewerId: authority.actorId, decidedAt: command.decidedAt, reason: command.reason, timeSeconds: command.timeSeconds, changedVariable: command.changedVariable, evidenceIds, storyPurposeVerified: command.storyPurposeVerified, }; const decision: CinemaReviewDecision = { ...decisionInput, contentHash: cinemaReviewDecisionHash(decisionInput) }; return appendCinemaReviewDecision(input.root, command.projectSlug, decision, executedAt); }