import { Body, Controller, Get, Post, Query, UseGuards } from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  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 { TaskService } from '../task.service.js';
import { PublishTaskDto } from '../dto/publish-task.dto.js';
import { AcceptTaskDto } from '../dto/accept-task.dto.js';
import { CompleteTaskDto } from '../dto/complete-task.dto.js';
import { CancelTaskDto } from '../dto/cancel-task.dto.js';
import { TaskRecordQueryDto } from '../dto/task-record-query.dto.js';
import { AssignableEngineerQueryDto } from '../dto/assignable-engineer-query.dto.js';

/**
 * B 端任务控制器：封装调压任务状态流转接口。
 */
@ApiTags('调压任务(B端)')
@ApiBearerAuth('bearer')
@Controller('b/tasks')
@UseGuards(AccessTokenGuard, RolesGuard)
export class BTasksController {
  constructor(private readonly taskService: TaskService) {}

  /**
   * 查询当前角色可见的医院工程师列表。
   */
  @Get('engineers')
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DOCTOR,
    Role.DIRECTOR,
    Role.LEADER,
  )
  @ApiOperation({ summary: '查询医院工程师列表' })
  @ApiQuery({
    name: 'hospitalId',
    required: false,
    description: '系统管理员可按医院筛选',
  })
  findAssignableEngineers(
    @CurrentActor() actor: ActorContext,
    @Query() query: AssignableEngineerQueryDto,
  ) {
    return this.taskService.findAssignableEngineers(actor, query.hospitalId);
  }

  /**
   * 查询当前角色可见的调压记录列表。
   */
  @Get()
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DOCTOR,
    Role.DIRECTOR,
    Role.LEADER,
    Role.ENGINEER,
  )
  @ApiOperation({ summary: '查询调压记录列表' })
  @ApiQuery({
    name: 'hospitalId',
    required: false,
    description: '系统管理员可按医院筛选',
  })
  findRecords(
    @CurrentActor() actor: ActorContext,
    @Query() query: TaskRecordQueryDto,
  ) {
    return this.taskService.findTaskRecords(actor, query);
  }

  /**
   * 系统管理员/医院管理员/医生/主任/组长发布调压任务。
   */
  @Post('publish')
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DOCTOR,
    Role.DIRECTOR,
    Role.LEADER,
  )
  @ApiOperation({
    summary: '发布任务（SYSTEM_ADMIN/HOSPITAL_ADMIN/DOCTOR/DIRECTOR/LEADER）',
  })
  publish(@CurrentActor() actor: ActorContext, @Body() dto: PublishTaskDto) {
    return this.taskService.publishTask(actor, dto);
  }

  /**
   * 工程师接收调压任务。
   */
  @Post('accept')
  @Roles(Role.ENGINEER)
  @ApiOperation({ summary: '接收任务（ENGINEER）' })
  accept(@CurrentActor() actor: ActorContext, @Body() dto: AcceptTaskDto) {
    return this.taskService.acceptTask(actor, dto);
  }

  /**
   * 工程师完成调压任务。
   */
  @Post('complete')
  @Roles(Role.ENGINEER)
  @ApiOperation({ summary: '完成任务（ENGINEER）' })
  complete(@CurrentActor() actor: ActorContext, @Body() dto: CompleteTaskDto) {
    return this.taskService.completeTask(actor, dto);
  }

  /**
   * 系统管理员/医院管理员/医生/主任/组长可取消自己创建的任务；工程师可取消自己已接收的任务。
   */
  @Post('cancel')
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DOCTOR,
    Role.DIRECTOR,
    Role.LEADER,
    Role.ENGINEER,
  )
  @ApiOperation({
    summary:
      '取消任务（SYSTEM_ADMIN/HOSPITAL_ADMIN/DOCTOR/DIRECTOR/LEADER/ENGINEER）',
  })
  cancel(@CurrentActor() actor: ActorContext, @Body() dto: CancelTaskDto) {
    return this.taskService.cancelTask(actor, dto);
  }
}
