import {
  Controller,
  Delete,
  Get,
  HttpCode,
  HttpStatus,
  Param,
  ParseUUIDPipe,
  UseGuards,
} from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';

import {
  CurrentSession,
  CurrentUser,
} from './decorators/current-user.decorator';
import { SessionGuard } from './guards/session.guard';
import { SessionService } from './session.service';

@ApiTags('me')
@Controller('me')
@UseGuards(SessionGuard)
export class MeController {
  constructor(private readonly sessionService: SessionService) {}

  @Get()
  @ApiOperation({ summary: 'Get current user profile' })
  @ApiResponse({ status: 200, description: 'Current user' })
  getMe(@CurrentUser() user: { id: string }) {
    return { user };
  }

  @Get('sessions')
  @ApiOperation({ summary: 'List active sessions' })
  async listSessions(
    @CurrentUser() user: { id: string },
    @CurrentSession() session: { sessionId: string },
  ) {
    const sessions = await this.sessionService.listSessions(
      user.id,
      session.sessionId,
    );
    return { sessions };
  }

  @Delete('sessions/:id')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Revoke a session' })
  async revokeSession(
    @CurrentUser() user: { id: string },
    @Param('id', ParseUUIDPipe) sessionId: string,
  ): Promise<void> {
    await this.sessionService.revokeSession(sessionId, user.id);
  }
}
