import {
  Controller,
  DefaultValuePipe,
  Get,
  ParseIntPipe,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { CurrentUser } from '../auth/decorators/current-user.decorator';
import type { PublicUser } from '../user/user.service';
import { TaskService, type MyTaskRowResponse } from './task.service';
import type { PaginatedResult } from '../../common/pagination/pagination';

// 내 업무(프로젝트 횡단) — 담당/지시 집계. 그룹핑·라벨은 프론트가 파생
@ApiTags('MyTask')
@Controller('tasks')
@UseGuards(JwtAuthGuard)
export class MyTaskController {
  constructor(private readonly taskService: TaskService) {}

  @Get('assigned')
  @ApiOperation({
    summary:
      '내가 담당인 업무 (프로젝트 횡단, 검색/프로젝트/정렬 + 페이지네이션)',
  })
  @ApiResponse({ status: 200, description: '조회 성공' })
  listAssigned(
    @CurrentUser() user: PublicUser,
    @Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number,
    @Query('size', new DefaultValuePipe(20), ParseIntPipe) size: number,
    @Query('q') q?: string,
    @Query('projectId') projectId?: string,
    @Query('sort') sort?: string,
  ): Promise<PaginatedResult<MyTaskRowResponse>> {
    return this.taskService.listAssigned(user.id, page, size, {
      q,
      projectId,
      sort,
    });
  }

  @Get('issued')
  @ApiOperation({
    summary:
      '내가 지시한 업무 (프로젝트 횡단, 검색/프로젝트/정렬 + 페이지네이션)',
  })
  @ApiResponse({ status: 200, description: '조회 성공' })
  listIssued(
    @CurrentUser() user: PublicUser,
    @Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number,
    @Query('size', new DefaultValuePipe(20), ParseIntPipe) size: number,
    @Query('q') q?: string,
    @Query('projectId') projectId?: string,
    @Query('sort') sort?: string,
  ): Promise<PaginatedResult<MyTaskRowResponse>> {
    return this.taskService.listIssued(user.id, page, size, {
      q,
      projectId,
      sort,
    });
  }
}
