import {
  Body,
  Controller,
  Get,
  Param,
  ParseIntPipe,
  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';

@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);
  }

  @Get('items/:id')
  @Roles(
    Role.SYSTEM_ADMIN,
    Role.HOSPITAL_ADMIN,
    Role.DOCTOR,
    Role.DIRECTOR,
    Role.LEADER,
    Role.ENGINEER,
  )
  @ApiOperation({ summary: '查询单条调压记录详情' })
  @ApiQuery({
    name: 'id',
    required: true,
    description: '调压记录 ID (taskItem.id)',
  })
  findRecordItem(
    @CurrentActor() actor: ActorContext,
    @Param('id', ParseIntPipe) id: number,
  ) {
    return this.taskService.getTaskRecordItem(actor, id);
  }

  @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);
  }
}
