import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseIntPipe,
  Patch,
  Post,
  Query,
  UseGuards,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  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 { CreateImplantCatalogDto } from '../dto/create-implant-catalog.dto.js';
import { CreateDeviceDto } from '../dto/create-device.dto.js';
import { DeviceQueryDto } from '../dto/device-query.dto.js';
import { UpdateImplantCatalogDto } from '../dto/update-implant-catalog.dto.js';
import { UpdateDeviceDto } from '../dto/update-device.dto.js';
import { DevicesService } from '../devices.service.js';

/**
 * B 端设备控制器：仅管理员可访问设备 CRUD。
 */
@ApiTags('设备管理(B端)')
@ApiBearerAuth('bearer')
@Controller('b/devices')
@UseGuards(AccessTokenGuard, RolesGuard)
export class BDevicesController {
  constructor(private readonly devicesService: DevicesService) {}

  /**
   * 查询可见植入物型号字典。
   */
  @Get('catalogs')
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DIRECTOR,
    Role.LEADER,
    Role.DOCTOR,
    Role.ENGINEER,
  )
  @ApiOperation({ summary: '查询植入物型号字典' })
  @ApiQuery({
    name: 'keyword',
    required: false,
    description: '支持按型号、厂家、名称模糊查询',
  })
  findCatalogs(
    @CurrentActor() actor: ActorContext,
    @Query('keyword') keyword?: string,
  ) {
    return this.devicesService.findCatalogs(actor, keyword);
  }

  /**
   * 新增植入物型号字典。
   */
  @Post('catalogs')
  @Roles(Role.SYSTEM_ADMIN)
  @ApiOperation({ summary: '新增植入物目录（SYSTEM_ADMIN）' })
  createCatalog(
    @CurrentActor() actor: ActorContext,
    @Body() dto: CreateImplantCatalogDto,
  ) {
    return this.devicesService.createCatalog(actor, dto);
  }

  /**
   * 更新植入物型号字典。
   */
  @Patch('catalogs/:id')
  @Roles(Role.SYSTEM_ADMIN)
  @ApiOperation({ summary: '更新植入物目录（SYSTEM_ADMIN）' })
  @ApiParam({ name: 'id', description: '型号字典 ID' })
  updateCatalog(
    @CurrentActor() actor: ActorContext,
    @Param('id', ParseIntPipe) id: number,
    @Body() dto: UpdateImplantCatalogDto,
  ) {
    return this.devicesService.updateCatalog(actor, id, dto);
  }

  /**
   * 删除植入物目录。
   */
  @Delete('catalogs/:id')
  @Roles(Role.SYSTEM_ADMIN)
  @ApiOperation({ summary: '删除植入物目录（SYSTEM_ADMIN）' })
  @ApiParam({ name: 'id', description: '型号字典 ID' })
  removeCatalog(
    @CurrentActor() actor: ActorContext,
    @Param('id', ParseIntPipe) id: number,
  ) {
    return this.devicesService.removeCatalog(actor, id);
  }

  /**
   * 查询设备列表。
   */
  @Get()
  @Roles(Role.SYSTEM_ADMIN, Role.HOSPITAL_ADMIN)
  @ApiOperation({ summary: '查询设备列表' })
  findAll(@CurrentActor() actor: ActorContext, @Query() query: DeviceQueryDto) {
    return this.devicesService.findAll(actor, query);
  }

  /**
   * 查询设备详情。
   */
  @Get(':id')
  @Roles(Role.SYSTEM_ADMIN, Role.HOSPITAL_ADMIN)
  @ApiOperation({ summary: '查询设备详情' })
  @ApiParam({ name: 'id', description: '设备 ID' })
  findOne(
    @CurrentActor() actor: ActorContext,
    @Param('id', ParseIntPipe) id: number,
  ) {
    return this.devicesService.findOne(actor, id);
  }

  /**
   * 创建设备。
   */
  @Post()
  @Roles(Role.SYSTEM_ADMIN, Role.HOSPITAL_ADMIN)
  @ApiOperation({ summary: '创建设备' })
  create(@CurrentActor() actor: ActorContext, @Body() dto: CreateDeviceDto) {
    return this.devicesService.create(actor, dto);
  }

  /**
   * 更新设备。
   */
  @Patch(':id')
  @Roles(Role.SYSTEM_ADMIN, Role.HOSPITAL_ADMIN)
  @ApiOperation({ summary: '更新设备' })
  @ApiParam({ name: 'id', description: '设备 ID' })
  update(
    @CurrentActor() actor: ActorContext,
    @Param('id', ParseIntPipe) id: number,
    @Body() dto: UpdateDeviceDto,
  ) {
    return this.devicesService.update(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' })
  remove(
    @CurrentActor() actor: ActorContext,
    @Param('id', ParseIntPipe) id: number,
  ) {
    return this.devicesService.remove(actor, id);
  }
}
