/**
 * Publish model for marketplace design/content: draft -> validate -> preview -> publish.
 * Contract: docs/backend/PHASE-9-TENANT-REGISTRY-DOMAINS-CONTRACT.md §5.
 *
 * Hard invariant, stated in the contract and repeated here because it is
 * the whole point of this model existing separately from commerce data:
 * Order, Payment, InventoryRecord, and every financial ledger row are NOT
 * part of a MarketplaceRevision. Rolling back a design revision must never
 * touch commerce data. Nothing in this model, and nothing that consumes it,
 * should ever reference an order, payment, or inventory record.
 */
/**
 * §5 describes the pipeline as draft -> validation -> preview -> publish,
 * but only lists 3 write endpoints (validate, publish, rollback) for these
 * 4 stages - there is no dedicated "move to preview" call. This model
 * assumes POST .../validate moves a revision straight to 'preview' (the
 * state publish() actually requires), treating 'validated' as a transient
 * value the caller may never observe rather than a distinct stored status.
 * Confirm against the real backend response before relying on 'validated'
 * ever being read back.
 */
export type RevisionStatus = 'draft' | 'validated' | 'preview' | 'published';

export interface MarketplaceRevision {
  id: string;
  marketplaceId: string;
  status: RevisionStatus;
  publishedAt?: string;
  /** Rollback creates a NEW revision pointing here - the old one is never mutated. */
  supersedesRevisionId?: string;
}

/** Only forward transitions in the pipeline, plus rollback (which is a new revision, not a transition). */
export function canValidate(revision: MarketplaceRevision): boolean {
  return revision.status === 'draft';
}

export function canPreview(revision: MarketplaceRevision): boolean {
  return revision.status === 'validated';
}

export function canPublish(revision: MarketplaceRevision): boolean {
  return revision.status === 'preview';
}

/** A published revision is immutable (§5) - rollback is the only way to move past it, and that creates a new revision. */
export function isImmutable(revision: MarketplaceRevision): boolean {
  return revision.status === 'published';
}
