import type { IncidentSeverity, IncidentStatus } from "@/modules/core/types";

export const INCIDENT_SEVERITY_VALUES = ["info", "warning", "critical"] as const satisfies readonly IncidentSeverity[];
export const INCIDENT_STATUS_VALUES = ["open", "acknowledged", "resolved", "closed"] as const satisfies readonly IncidentStatus[];

export const INCIDENT_SEVERITY_LABELS: Record<IncidentSeverity, string> = {
  info: "提示",
  warning: "预警",
  critical: "紧急",
};

export const INCIDENT_STATUS_LABELS: Record<IncidentStatus, string> = {
  open: "待处理",
  acknowledged: "已确认",
  resolved: "已解决",
  closed: "已关闭",
};

export type EmergencyIncident = {
  id: string;
  organizationId?: string;
  severity: IncidentSeverity;
  status: IncidentStatus;
  title: string;
  description: string;
  source: string;
  acknowledgedByAccountId?: string;
  acknowledgedByName?: string;
  acknowledgedAt?: string;
  resolvedByAccountId?: string;
  resolvedByName?: string;
  resolvedAt?: string;
  createdAt: string;
  updatedAt: string;
};

export type EmergencyIncidentMetrics = {
  open: number;
  acknowledged: number;
  critical: number;
  resolvedToday: number;
};

export type EmergencyIncidentData = {
  incidents: EmergencyIncident[];
  metrics: EmergencyIncidentMetrics;
};

export type EmergencyIncidentCreateInput = {
  severity: IncidentSeverity;
  title: string;
  description: string;
  source: string;
};

export type EmergencyIncidentStatusUpdateInput = {
  status: IncidentStatus;
};

type ValidationSuccess<T> = {
  success: true;
  data: T;
};

type ValidationFailure = {
  success: false;
  reason: string;
};

export type ValidationResult<T> = ValidationSuccess<T> | ValidationFailure;

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}

function readString(source: Record<string, unknown>, key: string): string {
  const value = source[key];
  return typeof value === "string" ? value.trim() : "";
}

function readEnum<T extends string>(value: unknown, values: readonly T[]): T | null {
  return typeof value === "string" ? values.find((item) => item === value) ?? null : null;
}

export function validateEmergencyIncidentCreateInput(value: unknown): ValidationResult<EmergencyIncidentCreateInput> {
  if (!isRecord(value)) {
    return { success: false, reason: "请求数据格式无效" };
  }

  const severity = readEnum(value.severity, INCIDENT_SEVERITY_VALUES);
  if (!severity) {
    return { success: false, reason: "事件级别无效" };
  }

  const title = readString(value, "title");
  const description = readString(value, "description");
  if (!title || !description) {
    return { success: false, reason: "事件标题和描述不能为空" };
  }

  return {
    success: true,
    data: {
      severity,
      title,
      description,
      source: readString(value, "source") || "人工上报",
    },
  };
}

export function validateEmergencyIncidentStatusUpdateInput(value: unknown): ValidationResult<EmergencyIncidentStatusUpdateInput> {
  if (!isRecord(value)) {
    return { success: false, reason: "请求数据格式无效" };
  }

  const status = readEnum(value.status, INCIDENT_STATUS_VALUES);
  if (!status) {
    return { success: false, reason: "事件状态无效" };
  }

  return { success: true, data: { status } };
}
