import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseIntPipe,
  Patch,
  Post,
  Query,
  UseGuards,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiExtraModels,
  ApiOkResponse,
  ApiOperation,
  ApiParam,
  ApiQuery,
  ApiTags,
} from '@nestjs/swagger';
import { AccessTokenGuard } from '../../auth/access-token.guard.js';
import { CurrentActor } from '../../auth/current-actor.decorator.js';
import { Roles } from '../../auth/roles.decorator.js';
import { RolesGuard } from '../../auth/roles.guard.js';
import type { ActorContext } from '../../common/actor-context.js';
import { Role } from '../../generated/prisma/enums.js';
import { CreatePatientDto } from '../dto/create-patient.dto.js';
import { CreatePatientSurgeryDto } from '../dto/create-patient-surgery.dto.js';
import { PatientQueryDto } from '../dto/patient-query.dto.js';
import {
  BPatientLifecycleResponseDto,
  PATIENT_LIFECYCLE_SWAGGER_MODELS,
} from '../dto/patient-lifecycle-response.dto.js';
import { UpdatePatientSurgeryDto } from '../dto/update-patient-surgery.dto.js';
import { UpdatePatientDto } from '../dto/update-patient.dto.js';
import { BPatientsService } from './b-patients.service.js';

@ApiTags('患者管理(B端)')
@ApiBearerAuth('bearer')
@Controller('b/patients')
@UseGuards(AccessTokenGuard, RolesGuard)
export class BPatientsController {
  constructor(private readonly patientsService: BPatientsService) {}

  @Get('doctors')
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DIRECTOR,
    Role.LEADER,
    Role.DOCTOR,
  )
  @ApiOperation({ summary: '查询当前登录人可见的医生列表' })
  @ApiQuery({
    name: 'hospitalId',
    required: false,
    description: '医院编号，仅系统管理员可选传',
  })
  findVisibleDoctors(
    @CurrentActor() actor: ActorContext,
    @Query('hospitalId') hospitalId?: string,
  ) {
    const requestedHospitalId =
      hospitalId == null || hospitalId === '' ? undefined : Number(hospitalId);
    return this.patientsService.findVisibleDoctors(actor, requestedHospitalId);
  }

  @Get()
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DIRECTOR,
    Role.LEADER,
    Role.DOCTOR,
  )
  @ApiOperation({ summary: '按服务端分页查询当前登录人可见的患者列表' })
  findVisiblePatients(
    @CurrentActor() actor: ActorContext,
    @Query() query: PatientQueryDto,
  ) {
    return this.patientsService.findVisiblePatients(actor, query);
  }

  @Post()
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DIRECTOR,
    Role.LEADER,
    Role.DOCTOR,
  )
  @ApiOperation({ summary: '创建患者档案' })
  createPatient(
    @CurrentActor() actor: ActorContext,
    @Body() dto: CreatePatientDto,
  ) {
    return this.patientsService.createPatient(actor, dto);
  }

  @Post(':id/surgeries')
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DIRECTOR,
    Role.LEADER,
    Role.DOCTOR,
  )
  @ApiOperation({ summary: '为患者新增手术记录' })
  @ApiParam({ name: 'id', description: '患者编号' })
  createPatientSurgery(
    @CurrentActor() actor: ActorContext,
    @Param('id', ParseIntPipe) id: number,
    @Body() dto: CreatePatientSurgeryDto,
  ) {
    return this.patientsService.createPatientSurgery(actor, id, dto);
  }

  @Patch(':id/surgeries/:surgeryId')
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DIRECTOR,
    Role.LEADER,
    Role.DOCTOR,
  )
  @ApiOperation({ summary: '更新患者手术记录' })
  @ApiParam({ name: 'id', description: '患者编号' })
  @ApiParam({ name: 'surgeryId', description: '手术编号' })
  updatePatientSurgery(
    @CurrentActor() actor: ActorContext,
    @Param('id', ParseIntPipe) id: number,
    @Param('surgeryId', ParseIntPipe) surgeryId: number,
    @Body() dto: UpdatePatientSurgeryDto,
  ) {
    return this.patientsService.updatePatientSurgery(actor, id, surgeryId, dto);
  }

  @Get(':id/lifecycle')
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DIRECTOR,
    Role.LEADER,
    Role.DOCTOR,
  )
  @ApiOperation({ summary: '查询患者生命周期事件（B端详情页）' })
  @ApiParam({ name: 'id', description: '患者编号' })
  @ApiExtraModels(...PATIENT_LIFECYCLE_SWAGGER_MODELS)
  @ApiOkResponse({
    description: '返回指定患者的生命周期数据',
    type: BPatientLifecycleResponseDto,
  })
  findPatientLifecycle(
    @CurrentActor() actor: ActorContext,
    @Param('id', ParseIntPipe) id: number,
  ) {
    return this.patientsService.findPatientLifecycle(actor, id);
  }

  @Get(':id')
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DIRECTOR,
    Role.LEADER,
    Role.DOCTOR,
  )
  @ApiOperation({ summary: '查询患者详情' })
  @ApiParam({ name: 'id', description: '患者编号' })
  findPatientById(
    @CurrentActor() actor: ActorContext,
    @Param('id', ParseIntPipe) id: number,
  ) {
    return this.patientsService.findPatientById(actor, id);
  }

  @Patch(':id')
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DIRECTOR,
    Role.LEADER,
    Role.DOCTOR,
  )
  @ApiOperation({ summary: '更新患者档案' })
  @ApiParam({ name: 'id', description: '患者编号' })
  updatePatient(
    @CurrentActor() actor: ActorContext,
    @Param('id', ParseIntPipe) id: number,
    @Body() dto: UpdatePatientDto,
  ) {
    return this.patientsService.updatePatient(actor, id, dto);
  }

  @Delete(':id')
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DIRECTOR,
    Role.LEADER,
    Role.DOCTOR,
  )
  @ApiOperation({ summary: '删除患者档案' })
  @ApiParam({ name: 'id', description: '患者编号' })
  removePatient(
    @CurrentActor() actor: ActorContext,
    @Param('id', ParseIntPipe) id: number,
  ) {
    return this.patientsService.removePatient(actor, id);
  }
}
