import { Controller, Get, Post, Body, Param, Query } from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { LearningSessionService } from './learning-session.service';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { PaginationDto } from '../../common/dto/pagination.dto';
import { StartSessionDto } from './dto/start-session.dto';
import type { UserPayload } from '../../common/types';

@ApiTags('learning-session')
@Controller('learning-sessions')
export class LearningSessionController {
  constructor(private readonly service: LearningSessionService) {}

  @Post()
  @ApiOperation({ summary: '开始学习会话' })
  async start(@CurrentUser() user: UserPayload, @Body() dto: StartSessionDto) {
    return this.service.start(String(user?.id || 'anonymous'), dto);
  }

  @Post(':id/end')
  @ApiOperation({ summary: '结束学习会话' })
  async end(@Param('id') id: string) {
    return this.service.end(id);
  }

  @Get()
  @ApiOperation({ summary: '获取学习会话列表' })
  async findAll(
    @CurrentUser() user: UserPayload,
    @Query() pagination: PaginationDto,
    @Query('status') status?: string,
    @Query('sort') sort?: string,
  ) {
    return this.service.findByUserId(String(user?.id || 'anonymous'), {
      ...pagination,
      status,
      sort,
    });
  }
}
