import { Body, Controller, Param, Patch, UseGuards } from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiParam,
  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 { UsersService } from '../users.service.js';
import { AssignEngineerHospitalDto } from '../dto/assign-engineer-hospital.dto.js';

/**
 * B 端用户扩展控制器：承载非标准 CRUD 的组织授权接口。
 */
@ApiTags('用户管理(B端)')
@ApiBearerAuth('bearer')
@Controller('b/users')
@UseGuards(AccessTokenGuard, RolesGuard)
export class BUsersController {
  constructor(private readonly usersService: UsersService) {}

  /**
   * 将工程师绑定到指定医院（仅系统管理员）。
   */
  @Patch(':id/assign-engineer-hospital')
  @Roles(Role.SYSTEM_ADMIN)
  @ApiOperation({ summary: '绑定工程师到医院（SYSTEM_ADMIN）' })
  @ApiParam({ name: 'id', description: '工程师用户 ID' })
  assignEngineerHospital(
    @CurrentActor() actor: ActorContext,
    @Param('id') id: string,
    @Body() dto: AssignEngineerHospitalDto,
  ) {
    return this.usersService.assignEngineerHospital(actor, Number(id), dto);
  }
}
