// --- 患者 ---
export function createPatientFixture(overrides: Record<string, unknown> = {}) {
  return {
    id: 'patient-1',
    name: '张三',
    gender: 'male',
    birth_date: '1990-01-15',
    blood_type: 'A',
    status: 'active',
    verification_status: 'verified',
    source: 'manual',
    created_at: '2026-04-01T10:00:00Z',
    updated_at: '2026-04-01T10:00:00Z',
    version: 1,
    ...overrides,
  };
}

// --- 医生 ---
export function createDoctorFixture(overrides: Record<string, unknown> = {}) {
  return {
    id: 'doctor-1',
    user_id: 'user-doc-1',
    name: '李医生',
    department: '内科',
    title: '主治医师',
    specialization: '心血管内科',
    phone: '13800000001',
    email: 'doctor1@test.com',
    status: 'online',
    created_at: '2026-04-01T10:00:00Z',
    updated_at: '2026-04-01T10:00:00Z',
    version: 1,
    ...overrides,
  };
}

// --- 告警 ---
export function createAlertFixture(overrides: Record<string, unknown> = {}) {
  return {
    id: 'alert-1',
    patient_id: 'patient-1',
    patient_name: '张三',
    alert_type: 'vital_sign',
    severity: 'high',
    status: 'active',
    message: '血压异常偏高',
    created_at: '2026-04-01T10:00:00Z',
    updated_at: '2026-04-01T10:00:00Z',
    version: 1,
    ...overrides,
  };
}

// --- 预约 ---
export function createAppointmentFixture(overrides: Record<string, unknown> = {}) {
  return {
    id: 'appt-1',
    patient_id: 'patient-1',
    patient_name: '张三',
    doctor_id: 'doctor-1',
    doctor_name: '李医生',
    appointment_date: '2026-05-10',
    start_time: '09:00',
    end_time: '09:30',
    status: 'pending',
    type: 'follow_up',
    notes: '',
    created_at: '2026-04-01T10:00:00Z',
    updated_at: '2026-04-01T10:00:00Z',
    version: 1,
    ...overrides,
  };
}

// --- 设备 ---
export function createDeviceFixture(overrides: Record<string, unknown> = {}) {
  return {
    id: 'device-1',
    patient_id: 'patient-1',
    device_id: 'DEV-20260401-0001',
    device_model: 'BP Monitor Pro',
    device_type: 'blood_pressure',
    status: 'online',
    firmware_version: '1.2.3',
    manufacturer: 'HealthTech',
    connection_type: 'ble',
    bound_at: '2026-04-01T10:00:00Z',
    last_sync_at: '2026-04-02T08:30:00Z',
    version: 1,
    ...overrides,
  };
}

// --- AI 分析 ---
export function createAnalysisFixture(overrides: Record<string, unknown> = {}) {
  return {
    id: 'analysis-1',
    patient_id: 'patient-1',
    patient_name: '张三',
    analysis_type: 'lab_report_interpretation',
    source_ref: 'lab-report-1',
    model_used: 'gpt-4',
    status: 'completed',
    result_content: '## 分析结果\n- 血压正常范围\n- 血糖略高',
    result_metadata: null,
    error_message: null,
    created_at: '2026-04-01T10:00:00Z',
    updated_at: '2026-04-01T10:00:00Z',
    ...overrides,
  };
}

// --- 积分商品 ---
export function createPointsProductFixture(overrides: Record<string, unknown> = {}) {
  return {
    id: 'product-1',
    name: '体检套餐兑换券',
    product_type: 'service',
    points_cost: 100,
    stock: 50,
    is_active: true,
    description: '可兑换基础体检套餐',
    image_url: null,
    sort_order: 0,
    created_at: '2026-04-01T10:00:00Z',
    updated_at: '2026-04-01T10:00:00Z',
    version: 1,
    ...overrides,
  };
}

// --- 积分规则 ---
export function createPointsRuleFixture(overrides: Record<string, unknown> = {}) {
  return {
    id: 'rule-1',
    event_type: 'checkin',
    name: '每日打卡',
    description: '每日健康打卡获得积分',
    points_value: 10,
    daily_cap: 1,
    streak_7d_bonus: 20,
    streak_14d_bonus: 50,
    streak_30d_bonus: 100,
    is_active: true,
    created_at: '2026-04-01T10:00:00Z',
    updated_at: '2026-04-01T10:00:00Z',
    version: 1,
    ...overrides,
  };
}

// --- 积分订单 ---
export function createPointsOrderFixture(overrides: Record<string, unknown> = {}) {
  return {
    id: 'order-00112233-4455',
    patient_id: 'patient-1',
    product_id: 'product-1',
    product_name: '体检套餐兑换券',
    points_cost: 100,
    status: 'pending',
    qr_code: 'QR-ORDER-001',
    verified_at: null,
    verified_by: null,
    expires_at: '2026-06-01T00:00:00Z',
    notes: null,
    created_at: '2026-04-01T10:00:00Z',
    updated_at: '2026-04-01T10:00:00Z',
    version: 1,
    ...overrides,
  };
}

// --- 文章 ---
export function createArticleFixture(overrides: Record<string, unknown> = {}) {
  return {
    id: 'article-1',
    title: '健康饮食指南',
    summary: '如何通过饮食改善健康状况',
    content: '文章内容...',
    cover_image: null,
    category_id: 'cat-1',
    category_name: '营养健康',
    tags: [{ id: 'tag-1', name: '饮食' }],
    status: 'published',
    author: '李医生',
    view_count: 128,
    published_at: '2026-04-01T10:00:00Z',
    created_at: '2026-04-01T10:00:00Z',
    updated_at: '2026-04-01T10:00:00Z',
    version: 1,
    ...overrides,
  };
}

// --- 告警规则 ---
export function createAlertRuleFixture(overrides: Record<string, unknown> = {}) {
  return {
    id: 'rule-1',
    name: '血压偏高告警',
    description: '收缩压超过 140 时触发告警',
    device_type: 'blood_pressure',
    condition_type: 'threshold',
    condition_params: { direction: 'above', value: 140 },
    severity: 'high',
    cooldown_minutes: 60,
    is_active: true,
    created_at: '2026-04-01T10:00:00Z',
    updated_at: '2026-04-01T10:00:00Z',
    version: 1,
    ...overrides,
  };
}

// --- 排班 ---
export function createScheduleFixture(overrides: Record<string, unknown> = {}) {
  return {
    id: 'schedule-1',
    doctor_id: 'doctor-1',
    schedule_date: '2026-05-10',
    period_type: 'am',
    start_time: '08:00',
    end_time: '12:00',
    max_appointments: 10,
    current_appointments: 3,
    status: 'enabled',
    created_at: '2026-04-01T10:00:00Z',
    updated_at: '2026-04-01T10:00:00Z',
    version: 1,
    ...overrides,
  };
}

// --- 批量生成 ---
export function createFixtureList<T>(
  factory: (overrides?: Record<string, unknown>) => T,
  count: number,
  overridesList: Record<string, unknown>[] = [],
): T[] {
  return Array.from({ length: count }, (_, i) => {
    const autoId = { id: `${i + 1}` };
    return factory({ ...autoId, ...overridesList[i] });
  });
}

// --- 分页响应包装 ---
export function wrapPaginated<T>(items: T[], total?: number) {
  return {
    data: items,
    total: total ?? items.length,
    page: 1,
    page_size: 20,
    total_pages: Math.ceil((total ?? items.length) / 20),
  };
}
