import {
  ApiProperty,
  ApiPropertyOptional,
  getSchemaPath,
} from '@nestjs/swagger';
import { DeviceStatus, TaskStatus } from '../../generated/prisma/enums.js';

export class PatientLifecycleHospitalDto {
  @ApiProperty({ description: '医院 ID', example: 1 })
  id!: number;

  @ApiProperty({ description: '医院名称', example: '珠江医院' })
  name!: string;
}

export class PatientLifecyclePatientDto {
  @ApiProperty({ description: '患者 ID', example: 4 })
  id!: number;

  @ApiProperty({ description: '患者姓名', example: '测试222' })
  name!: string;

  @ApiProperty({
    description: '住院号',
    example: '123321',
    nullable: true,
    type: String,
  })
  inpatientNo!: string | null;

  @ApiProperty({
    description: '项目名称',
    example: '脑积水随访项目',
    nullable: true,
    type: String,
  })
  projectName!: string | null;
}

export class PatientLifecycleImplantCatalogDto {
  @ApiProperty({ description: '植入物型号 ID', example: 1 })
  id!: number;

  @ApiProperty({ description: '型号编码', example: '823832' })
  modelCode!: string;

  @ApiProperty({ description: '厂家', example: 'Codman' })
  manufacturer!: string;

  @ApiProperty({ description: '名称', example: '可调节压力分流管' })
  name!: string;

  @ApiProperty({ description: '是否为阀门', example: true })
  isValve!: boolean;

  @ApiProperty({
    description: '可选压力挡位列表',
    type: [String],
    example: ['30', '40', '50', '60', '70'],
  })
  pressureLevels!: string[];

  @ApiProperty({ description: '是否可调压', example: true })
  isPressureAdjustable!: boolean;

  @ApiProperty({
    description: '备注',
    example: null,
    nullable: true,
    type: String,
  })
  notes!: string | null;
}

export class PatientLifecycleDeviceSummaryDto {
  @ApiProperty({ description: '设备 ID', example: 3 })
  id!: number;

  @ApiProperty({ description: '设备状态', enum: DeviceStatus })
  status!: DeviceStatus;

  @ApiProperty({ description: '是否已弃用', example: false })
  isAbandoned!: boolean;

  @ApiProperty({ description: '当前压力挡位', example: '50' })
  currentPressure!: string;

  @ApiProperty({
    description: '植入型号快照',
    example: '823832',
    nullable: true,
    type: String,
  })
  implantModel!: string | null;

  @ApiProperty({
    description: '植入厂家快照',
    example: 'Codman',
    nullable: true,
    type: String,
  })
  implantManufacturer!: string | null;

  @ApiProperty({
    description: '植入名称快照',
    example: '可调节压力分流管',
    nullable: true,
    type: String,
  })
  implantName!: string | null;

  @ApiProperty({ description: '是否为阀门', example: true })
  isValve!: boolean;

  @ApiProperty({ description: '是否可调压', example: true })
  isPressureAdjustable!: boolean;
}

export class PatientLifecycleSurgeryDeviceDto extends PatientLifecycleDeviceSummaryDto {
  @ApiProperty({
    description: '初始压力挡位',
    example: '50',
    nullable: true,
    type: String,
  })
  initialPressure!: string | null;

  @ApiProperty({
    description: '分流方式',
    example: 'V_P',
    nullable: true,
    type: String,
  })
  shuntMode!: string | null;

  @ApiProperty({
    description: '远端分流方向',
    example: '右麦氏点',
    nullable: true,
    type: String,
  })
  distalShuntDirection!: string | null;

  @ApiProperty({
    description: '近端穿刺部位',
    type: [String],
    example: ['椎间隙'],
  })
  proximalPunctureAreas!: string[];

  @ApiProperty({
    description: '阀门放置位置',
    type: [String],
    example: ['耳后'],
  })
  valvePlacementSites!: string[];

  @ApiPropertyOptional({
    description: '植入物目录信息',
    type: PatientLifecycleImplantCatalogDto,
    nullable: true,
  })
  implantCatalog!: PatientLifecycleImplantCatalogDto | null;
}

export class PatientLifecycleSurgeryDetailDto {
  @ApiProperty({ description: '手术 ID', example: 3 })
  id!: number;

  @ApiProperty({
    description: '手术日期',
    example: '2026-01-13T16:00:00.000Z',
    format: 'date-time',
  })
  surgeryDate!: string;

  @ApiProperty({ description: '手术名称', example: '脑室腹' })
  surgeryName!: string;

  @ApiProperty({ description: '主刀医生姓名', example: '徐玉婷' })
  surgeonName!: string;

  @ApiProperty({ description: '原发病', example: '颅脑损伤' })
  primaryDisease!: string;

  @ApiProperty({
    description: '脑积水类型',
    type: [String],
    example: ['高压性'],
  })
  hydrocephalusTypes!: string[];

  @ApiProperty({
    description: '上次分流手术时间',
    example: null,
    nullable: true,
    format: 'date-time',
    type: String,
  })
  previousShuntSurgeryDate!: string | null;

  @ApiProperty({ description: '当前为第几次分流手术', example: 1 })
  shuntSurgeryCount!: number;
}

export class PatientLifecycleTaskSurgeryDto {
  @ApiProperty({ description: '手术 ID', example: 3 })
  id!: number;

  @ApiProperty({
    description: '手术日期',
    example: '2026-01-13T16:00:00.000Z',
    format: 'date-time',
  })
  surgeryDate!: string;

  @ApiProperty({ description: '手术名称', example: '脑室腹' })
  surgeryName!: string;
}

export class PatientLifecycleTaskDto {
  @ApiProperty({ description: '任务 ID', example: 5 })
  id!: number;

  @ApiProperty({ description: '任务状态', enum: TaskStatus })
  status!: TaskStatus;

  @ApiProperty({
    description: '任务创建时间',
    example: '2026-03-25T09:27:26.857Z',
    format: 'date-time',
  })
  createdAt!: string;
}

export class PatientLifecycleTaskItemDto {
  @ApiProperty({ description: '任务明细 ID', example: 5 })
  id!: number;

  @ApiProperty({ description: '原压力挡位', example: '50' })
  oldPressure!: string;

  @ApiProperty({ description: '目标压力挡位', example: '40' })
  targetPressure!: string;
}

export class PatientLifecycleSurgeryEventDto {
  @ApiProperty({ description: '事件类型', example: 'SURGERY' })
  eventType!: 'SURGERY';

  @ApiProperty({
    description: '事件发生时间',
    example: '2026-01-13T16:00:00.000Z',
    format: 'date-time',
  })
  occurredAt!: string;

  @ApiProperty({
    description: '所属医院',
    type: PatientLifecycleHospitalDto,
  })
  hospital!: PatientLifecycleHospitalDto;

  @ApiProperty({
    description: '手术详情',
    type: PatientLifecycleSurgeryDetailDto,
  })
  surgery!: PatientLifecycleSurgeryDetailDto;

  @ApiProperty({
    description: '本次手术植入设备列表',
    type: [PatientLifecycleSurgeryDeviceDto],
  })
  devices!: PatientLifecycleSurgeryDeviceDto[];
}

export class PatientLifecycleTaskAdjustmentEventDto {
  @ApiProperty({
    description: '事件类型',
    example: 'TASK_PRESSURE_ADJUSTMENT',
  })
  eventType!: 'TASK_PRESSURE_ADJUSTMENT';

  @ApiProperty({
    description: '事件发生时间',
    example: '2026-03-25T09:27:26.857Z',
    format: 'date-time',
  })
  occurredAt!: string;

  @ApiProperty({
    description: '所属医院',
    type: PatientLifecycleHospitalDto,
  })
  hospital!: PatientLifecycleHospitalDto;

  @ApiProperty({
    description: '关联设备',
    type: PatientLifecycleDeviceSummaryDto,
  })
  device!: PatientLifecycleDeviceSummaryDto;

  @ApiProperty({
    description: '关联手术摘要',
    type: PatientLifecycleTaskSurgeryDto,
    nullable: true,
  })
  surgery!: PatientLifecycleTaskSurgeryDto | null;

  @ApiProperty({ description: '任务主单', type: PatientLifecycleTaskDto })
  task!: PatientLifecycleTaskDto;

  @ApiProperty({
    description: '任务明细',
    type: PatientLifecycleTaskItemDto,
  })
  taskItem!: PatientLifecycleTaskItemDto;
}

export class CPatientLifecycleDataDto {
  @ApiProperty({ description: '当前登录手机号', example: '18027269840' })
  phone!: string;

  @ApiProperty({
    description: '当前手机号唯一命中的患者信息',
    type: PatientLifecyclePatientDto,
  })
  patient!: PatientLifecyclePatientDto;

  @ApiProperty({
    description: '按时间倒序排列的生命周期事件列表',
    type: 'array',
    items: {
      oneOf: [
        { $ref: getSchemaPath(PatientLifecycleSurgeryEventDto) },
        { $ref: getSchemaPath(PatientLifecycleTaskAdjustmentEventDto) },
      ],
    },
  })
  lifecycle!: Array<
    PatientLifecycleSurgeryEventDto | PatientLifecycleTaskAdjustmentEventDto
  >;
}

export class BPatientLifecycleDataDto {
  @ApiProperty({ description: '患者信息', type: PatientLifecyclePatientDto })
  patient!: PatientLifecyclePatientDto;

  @ApiProperty({
    description: '按时间倒序排列的生命周期事件列表',
    type: 'array',
    items: {
      oneOf: [
        { $ref: getSchemaPath(PatientLifecycleSurgeryEventDto) },
        { $ref: getSchemaPath(PatientLifecycleTaskAdjustmentEventDto) },
      ],
    },
  })
  lifecycle!: Array<
    PatientLifecycleSurgeryEventDto | PatientLifecycleTaskAdjustmentEventDto
  >;
}

export class CPatientLifecycleResponseDto {
  @ApiProperty({ description: '业务状态码', example: 0 })
  code!: number;

  @ApiProperty({ description: '响应消息', example: '成功' })
  msg!: string;

  @ApiProperty({
    description: '生命周期数据',
    type: CPatientLifecycleDataDto,
  })
  data!: CPatientLifecycleDataDto;
}

export class BPatientLifecycleResponseDto {
  @ApiProperty({ description: '业务状态码', example: 0 })
  code!: number;

  @ApiProperty({ description: '响应消息', example: '成功' })
  msg!: string;

  @ApiProperty({
    description: '生命周期数据',
    type: BPatientLifecycleDataDto,
  })
  data!: BPatientLifecycleDataDto;
}

export const PATIENT_LIFECYCLE_SWAGGER_MODELS = [
  PatientLifecycleHospitalDto,
  PatientLifecyclePatientDto,
  PatientLifecycleImplantCatalogDto,
  PatientLifecycleDeviceSummaryDto,
  PatientLifecycleSurgeryDeviceDto,
  PatientLifecycleSurgeryDetailDto,
  PatientLifecycleTaskSurgeryDto,
  PatientLifecycleTaskDto,
  PatientLifecycleTaskItemDto,
  PatientLifecycleSurgeryEventDto,
  PatientLifecycleTaskAdjustmentEventDto,
  CPatientLifecycleDataDto,
  BPatientLifecycleDataDto,
  CPatientLifecycleResponseDto,
  BPatientLifecycleResponseDto,
] as const;
