export const LIFECYCLE_MODEL_SCHEMA_VERSION = "lifecycle_model_v0_1" as const;

export const STAGE3_LIFECYCLE_DOMAINS = [
  "bank_settlement",
  "customer_settlement",
  "deferred_expense",
  "fixed_asset",
  "vat_flow",
  "period_close"
] as const;

export type LifecycleDomain = (typeof STAGE3_LIFECYCLE_DOMAINS)[number];

export type LifecycleStateClass = "initial" | "active" | "terminal" | "problematic";

export type LifecycleTransitionType = "expected" | "optional" | "forbidden";

export const LIFECYCLE_DEFECT_TYPES = [
  "missing_expected_transition",
  "invalid_transition",
  "stale_active_state",
  "contradictory_state",
  "premature_terminal_state",
  "misclosed_state",
  "orphan_intermediate_state",
  "cross_branch_state_conflict"
] as const;

export type LifecycleDefectType = (typeof LIFECYCLE_DEFECT_TYPES)[number];

export interface LifecycleStateDefinition {
  state_code: string;
  state_label: string;
  state_class: LifecycleStateClass;
  entry_conditions: string[];
  exit_conditions: string[];
  is_terminal: boolean;
  is_problematic: boolean;
  business_meaning: string;
}

export interface LifecycleTransitionDefinition {
  from_state: string;
  to_state: string;
  transition_type: LifecycleTransitionType;
  required_evidence: string[];
  optional_evidence: string[];
  forbidden_conditions: string[];
  business_meaning: string;
}

export interface LifecycleDefectDefinition {
  defect_code: LifecycleDefectType;
  defect_class: "timing" | "path" | "consistency" | "closure";
  severity_hint: "low" | "medium" | "high";
  business_meaning: string;
  evidence_requirements: string[];
  period_impact_potential: "none" | "indirect" | "direct";
}

export interface LifecycleDomainModel {
  schema_version: typeof LIFECYCLE_MODEL_SCHEMA_VERSION;
  lifecycle_domain: LifecycleDomain;
  lifecycle_object_types: string[];
  states: LifecycleStateDefinition[];
  transitions: LifecycleTransitionDefinition[];
  defects: LifecycleDefectDefinition[];
}

export type LifecycleConfidenceGrade = "low" | "medium" | "high";

export interface LifecycleConfidence {
  score: number;
  grade: LifecycleConfidenceGrade;
}

export interface LifecycleResolution {
  lifecycle_object_id: string;
  lifecycle_domain: LifecycleDomain;
  resolved_current_state: string;
  resolved_expected_state: string;
  resolved_previous_states: string[];
  missing_transitions: string[];
  invalid_transitions: string[];
  detected_defects: LifecycleDefectType[];
  state_confidence: LifecycleConfidence;
  resolution_evidence: string[];
  snapshot_limitations: string[];
}

