export interface User {
  username: string;
  password?: string;
  role: 'super' | 'admin' | 'user';
  name: string;
  phone?: string;
  email?: string;
  department?: string;
  status?: 'active' | 'inactive';
  createdAt?: string;
  visibleTemplates?: string[];
  manageableTemplates?: string[];
  signature?: string;
}

export interface Report {
  id: string;
  title: string;
  patientName: string;
  hospitalId: string;
  patientGender?: string;
  patientAge?: string;
  department?: string;
  bedNumber?: string;
  surgeryDate?: string;
  startHour?: string;
  startMinute?: string;
  endHour?: string;
  endMinute?: string;
  surgeon?: string[];
  assistant?: string[];
  anesthesiologist?: string[];
  anesthesiaType?: string;
  reportNote?: string;
  content: string;
  videos?: {
    id: string;
    name: string;
    url: string;
    duration: number;
  }[];
  capturedFrames?: CapturedFrame[];
  author: string;
  authorName: string;
  createdAt: string;
  updatedAt?: string;
  status: 'draft' | 'completed';
  history?: { content: string; updatedAt: string; updatedBy: string; action: 'save_draft' | 'complete_report' }[];
}

export interface CapturedFrame {
  id: number;
  videoIndex: number;
  videoName: string;
  time: number;
  timeFormatted: string;
  dataUrl: string;
  isManual?: boolean;
  manualOrder?: number;
}

export interface Template {
  id: string;
  name: string;
  desc?: string;
  content: string;
  createdAt: string;
  updatedAt?: string;
  author: string;
  fields?: FormField[];
}

export interface AiProviderConfig {
  endpoint: string;
  apiKey: string;
  modelName: string;
}

export interface XfSpeechConfig {
  appId: string;
  apiKey: string;
  apiSecret: string;
}

export const DEFAULT_XF_SPEECH: XfSpeechConfig = {
  appId: 'e0fe23e3',
  apiKey: '7fd08be316718c2280e85af4fe126306',
  apiSecret: 'ZGI5MjAzZDA0YzYwNDhjMWZiNTM2NDE0'
};

export interface SystemSettings {
  frameCount: number;
  framePositions: number[];
  defaultTemplate?: string;
  frameMode?: 'uniform' | 'keep';
  autoInsertFrames?: boolean;
  autoInsertFrameIndices?: number[];
  autoInsertDelay?: number;
  activeAiProvider: string;
  aiProviders: Record<string, AiProviderConfig>;
  xfSpeechConfig?: XfSpeechConfig;
}

export const DEFAULT_AI_PROVIDERS: Record<string, AiProviderConfig> = {
  kimi: { endpoint: 'https://api.moonshot.cn/v1', apiKey: '', modelName: 'moonshot-v1-32k-vision-preview' },
  deepseek: { endpoint: 'https://api.deepseek.com/v1', apiKey: '', modelName: 'deepseek-chat' },
  openai: { endpoint: 'https://api.openai.com/v1', apiKey: '', modelName: 'gpt-4o' },
  custom: { endpoint: '', apiKey: '', modelName: '' }
};

export interface BindableField {
  key: string;
  label: string;
}

export const BINDABLE_FIELDS: BindableField[] = [
  { key: 'patientName', label: '姓名' },
  { key: 'patientGender', label: '性别' },
  { key: 'patientAge', label: '年龄' },
  { key: 'department', label: '科别' },
  { key: 'bedNumber', label: '床号' },
  { key: 'hospitalId', label: '住院号' },
  { key: 'surgeryDate', label: '手术日期' },
  { key: 'title', label: '手术名称' },
  { key: 'startTime', label: '手术开始时间' },
  { key: 'endTime', label: '手术终止时间' },
  { key: 'surgeon', label: '手术者' },
  { key: 'assistant', label: '助手' },
  { key: 'anesthesiologist', label: '麻醉师' },
  { key: 'anesthesiaType', label: '麻醉方式' },
];

export type FieldType = 'text' | 'single_select' | 'multi_select' | 'time' | 'date' | 'signature' | 'image';

export interface FormField {
  key: string;
  label: string;
  category: string;
  type: FieldType;
  visibleInForm: boolean;
  isSystemLocked: boolean;
  options?: string[];
  timeFormat?: string;
  timeDefault?: 'current' | 'specific';
  fixedTimeValue?: string;
  hasUnderline?: boolean;
}

export const DEFAULT_FORM_FIELDS: FormField[] = [
  { key: 'patientName', label: '患者姓名', category: '填空', type: 'text', visibleInForm: true, isSystemLocked: true, hasUnderline: false },
  { key: 'hospitalId', label: '住院号', category: '填空', type: 'text', visibleInForm: true, isSystemLocked: true, hasUnderline: false },
  { key: 'title', label: '手术名称', category: '填空', type: 'text', visibleInForm: true, isSystemLocked: false },
  { key: 'patientGender', label: '患者性别', category: '单选', type: 'single_select', visibleInForm: true, isSystemLocked: false, options: ['男', '女'] },
  { key: 'patientAge', label: '患者年龄', category: '填空', type: 'text', visibleInForm: true, isSystemLocked: false },
  { key: 'department', label: '科别', category: '填空', type: 'text', visibleInForm: true, isSystemLocked: false },
  { key: 'bedNumber', label: '床号', category: '填空', type: 'text', visibleInForm: true, isSystemLocked: false },
  { key: 'surgeryDate', label: '手术日期', category: '时间', type: 'date', visibleInForm: true, isSystemLocked: true, timeFormat: 'YYYY-MM-DD', timeDefault: 'specific' },
  { key: 'startTime', label: '手术开始时间', category: '时间', type: 'time', visibleInForm: true, isSystemLocked: true, timeFormat: 'HH:mm', timeDefault: 'specific' },
  { key: 'endTime', label: '手术终止时间', category: '时间', type: 'time', visibleInForm: true, isSystemLocked: true, timeFormat: 'HH:mm', timeDefault: 'specific' },
  { key: 'reportDate', label: '撰写时间', category: '时间', type: 'date', visibleInForm: true, isSystemLocked: true, timeFormat: 'YYYY年MM月DD日', timeDefault: 'current' },
  { key: 'surgeon', label: '手术者', category: '多选', type: 'multi_select', visibleInForm: true, isSystemLocked: true, options: ['张医生', '李医生', '王医生'] },
  { key: 'assistant', label: '助手', category: '多选', type: 'multi_select', visibleInForm: true, isSystemLocked: true, options: ['赵医生', '钱医生', '孙医生'] },
  { key: 'anesthesiologist', label: '麻醉师', category: '多选', type: 'multi_select', visibleInForm: true, isSystemLocked: true, options: ['周医生', '吴医生', '郑医生'] },
  { key: 'anesthesiaType', label: '麻醉方式', category: '单选', type: 'single_select', visibleInForm: true, isSystemLocked: false, options: ['全麻', '局麻', '腰麻', '硬膜外麻醉', '静脉麻醉', '吸入麻醉'] },
  { key: 'preoperativeDiagnosis', label: '术前诊断', category: '单选', type: 'single_select', visibleInForm: true, isSystemLocked: true, options: ['胆囊结石伴慢性胆囊炎', '急性胆囊炎'] },
  { key: 'postoperativeDiagnosis', label: '术后诊断', category: '单选', type: 'single_select', visibleInForm: true, isSystemLocked: true, options: ['胆囊结石伴慢性胆囊炎', '急性胆囊炎'] },
  { key: 'postOpCondition', label: '手术后情况', category: '单选', type: 'single_select', visibleInForm: true, isSystemLocked: true, options: ['患者麻醉恢复后安返病房'] },
  { key: 'specimenDescription', label: '切除标本描述', category: '单选', type: 'single_select', visibleInForm: true, isSystemLocked: true, options: ['胆囊一枚，壁厚约0.3cm，内含数枚结石'] },
  { key: 'pathologyCheck', label: '是否送病理检查', category: '单选', type: 'single_select', visibleInForm: true, isSystemLocked: true, options: ['是', '否'] },
  { key: 'frozenPathology', label: '冰冻病理结果', category: '单选', type: 'single_select', visibleInForm: true, isSystemLocked: true, options: ['未见恶性', '待石蜡'] },
];
