/**
 * Evaluation Rule Domain Entity
 * 评估规则 — 管理员可配置的质量门控规则
 */

export const EvaluationRuleType = {
  FIELD_COMPLETENESS: 'FIELD_COMPLETENESS',
  ASSESSMENT_QUALITY: 'ASSESSMENT_QUALITY',
  RESPONSE_LENGTH: 'RESPONSE_LENGTH',
  MUST_CONTAIN: 'MUST_CONTAIN',
  STAGE_MIN_TURNS: 'STAGE_MIN_TURNS',
  CONVERSION_SIGNAL: 'CONVERSION_SIGNAL',
  TOPIC_BOUNDARY: 'TOPIC_BOUNDARY',
  NO_FABRICATION: 'NO_FABRICATION',
  LLM_JUDGE: 'LLM_JUDGE',
} as const;

export type EvaluationRuleTypeValue =
  (typeof EvaluationRuleType)[keyof typeof EvaluationRuleType];

export const EvaluationFailureAction = {
  RETRY: 'RETRY',
  SUPPLEMENT: 'SUPPLEMENT',
  WARN_AND_PASS: 'WARN_AND_PASS',
  ESCALATE: 'ESCALATE',
} as const;

export type EvaluationFailureActionValue =
  (typeof EvaluationFailureAction)[keyof typeof EvaluationFailureAction];

/** Severity ordering for resolving multiple failures */
const ACTION_SEVERITY: Record<EvaluationFailureActionValue, number> = {
  [EvaluationFailureAction.WARN_AND_PASS]: 0,
  [EvaluationFailureAction.SUPPLEMENT]: 1,
  [EvaluationFailureAction.RETRY]: 2,
  [EvaluationFailureAction.ESCALATE]: 3,
};

export function getHighestSeverityAction(
  actions: EvaluationFailureActionValue[],
): EvaluationFailureActionValue {
  if (actions.length === 0) return EvaluationFailureAction.WARN_AND_PASS;
  return actions.reduce((highest, current) =>
    ACTION_SEVERITY[current] > ACTION_SEVERITY[highest] ? current : highest,
  );
}

export class EvaluationRuleEntity {
  readonly id: string;
  tenantId: string | null;
  stage: string;
  ruleType: EvaluationRuleTypeValue;
  name: string;
  description: string | null;
  config: Record<string, unknown>;
  enabled: boolean;
  priority: number;
  failureAction: EvaluationFailureActionValue;
  createdBy: string | null;
  updatedBy: string | null;
  readonly createdAt: Date;
  updatedAt: Date;

  private constructor(props: {
    id: string;
    tenantId: string | null;
    stage: string;
    ruleType: EvaluationRuleTypeValue;
    name: string;
    description: string | null;
    config: Record<string, unknown>;
    enabled: boolean;
    priority: number;
    failureAction: EvaluationFailureActionValue;
    createdBy: string | null;
    updatedBy: string | null;
    createdAt: Date;
    updatedAt: Date;
  }) {
    Object.assign(this, props);
  }

  static create(props: {
    id: string;
    tenantId: string | null;
    stage: string;
    ruleType: EvaluationRuleTypeValue;
    name: string;
    description?: string;
    config: Record<string, unknown>;
    priority?: number;
    failureAction?: EvaluationFailureActionValue;
    createdBy?: string;
  }): EvaluationRuleEntity {
    const now = new Date();
    return new EvaluationRuleEntity({
      id: props.id,
      tenantId: props.tenantId,
      stage: props.stage,
      ruleType: props.ruleType,
      name: props.name,
      description: props.description || null,
      config: props.config,
      enabled: true,
      priority: props.priority ?? 0,
      failureAction: props.failureAction ?? EvaluationFailureAction.WARN_AND_PASS,
      createdBy: props.createdBy || null,
      updatedBy: null,
      createdAt: now,
      updatedAt: now,
    });
  }

  static fromPersistence(props: {
    id: string;
    tenantId: string | null;
    stage: string;
    ruleType: EvaluationRuleTypeValue;
    name: string;
    description: string | null;
    config: Record<string, unknown>;
    enabled: boolean;
    priority: number;
    failureAction: EvaluationFailureActionValue;
    createdBy: string | null;
    updatedBy: string | null;
    createdAt: Date;
    updatedAt: Date;
  }): EvaluationRuleEntity {
    return new EvaluationRuleEntity(props);
  }

  toggle(): void {
    this.enabled = !this.enabled;
    this.updatedAt = new Date();
  }

  isActive(): boolean {
    return this.enabled;
  }
}
