import { z } from 'zod'
import { ActivityEventSchema, type ActivityEvent } from './activity-events'

/**
 * Stateful trait — the (pipeline_key, stage_key, state_key, activity_log) quartet
 * applied to acq_deals (CRM HITL, shipped 2026-04-27) and being generalized to
 * acq_lists / acq_list_members / acq_list_companies via Track B.
 */
export interface Stateful {
  pipeline_key: string
  stage_key: string
  state_key: string
  activity_log: ActivityEvent[]
}

export const StatefulSchema = z.object({
  pipeline_key: z.string(),
  stage_key: z.string(),
  state_key: z.string(),
  activity_log: z.array(ActivityEventSchema)
})

/** Generic transition shape — concrete per-entity transitionItem implementations satisfy this. */
export type TransitionItem<T extends Stateful, TEvent extends ActivityEvent> = (
  item: T,
  transition: { stage_key?: string; state_key?: string; event: TEvent }
) => T

/** Generic action-derivation shape — concrete per-entity deriveActions implementations satisfy this. */
export type DeriveActions<T extends Stateful, TAction> = (item: T) => TAction[]
