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

/**
 * B 端患者控制器：院内可见性隔离查询。
 */
@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: '按角色查询可见患者列表' })
  @ApiQuery({
    name: 'hospitalId',
    required: false,
    description: '系统管理员可显式指定医院',
  })
  findVisiblePatients(
    @CurrentActor() actor: ActorContext,
    @Query('hospitalId') hospitalId?: string,
  ) {
    const requestedHospitalId =
      hospitalId == null || hospitalId === '' ? undefined : Number(hospitalId);

    return this.patientsService.findVisiblePatients(actor, requestedHospitalId);
  }

  /**
   * 创建患者。
   */
  @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: '患者 ID' })
  createPatientSurgery(
    @CurrentActor() actor: ActorContext,
    @Param('id', ParseIntPipe) id: number,
    @Body() dto: CreatePatientSurgeryDto,
  ) {
    return this.patientsService.createPatientSurgery(actor, id, dto);
  }

  /**
   * 查询患者详情。
   */
  @Get(':id')
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DIRECTOR,
    Role.LEADER,
    Role.DOCTOR,
  )
  @ApiOperation({ summary: '查询患者详情' })
  @ApiParam({ name: 'id', description: '患者 ID' })
  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: '患者 ID' })
  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: '患者 ID' })
  removePatient(
    @CurrentActor() actor: ActorContext,
    @Param('id', ParseIntPipe) id: number,
  ) {
    return this.patientsService.removePatient(actor, id);
  }
}
