/**
 * Compatibility facade for support state machines.
 *
 * The pure deciders live in src/server/domain/support-case/.
 */

import {
  decideCase,
  decideTicket,
  listLegalCaseTransitions,
  listLegalTicketTransitions,
} from '@/server/domain/support-case/machine.js';
import type {
  CaseCategory,
  CaseStatus,
  CaseTransitionContext,
  TicketState,
  TicketTransitionContext as TransitionContext,
} from '@/server/domain/support-case/events.js';

export type { CaseCategory, CaseStatus, TransitionContext, TicketState };
export { listLegalCaseTransitions, listLegalTicketTransitions };

export function canTransitionTicket(
  from: TicketState,
  to: TicketState,
  ctx: TransitionContext,
): { ok: true } | { ok: false; reason: string } {
  const result = decideTicket(
    { state: from },
    {
      kind: 'ticket_transition_requested',
      ticketId: '',
      to,
      actorType: ctx.actorType,
      actorId: null,
      reopenCount: ctx.reopenCount,
      reopenLimit: ctx.reopenLimit,
    },
  );
  return result.ok ? { ok: true } : { ok: false, reason: result.message };
}

export function canTransitionCase(
  from: CaseStatus,
  to: CaseStatus,
  ctx: CaseTransitionContext = {},
): boolean {
  return decideCase(
    { state: from },
    {
      kind: 'case_transition_requested',
      caseId: '',
      to,
      actorType: 'system',
      actorId: null,
      ctx,
    },
  ).ok;
}
