import { BadRequestException, Body, Controller, Get, Post, Query } from '@nestjs/common';
import {
  createSaintSchema,
  listSaintsQuerySchema,
  type CreateSaintInput,
  type AuthUser,
} from '@vihar/shared';
import { SaintsService } from './saints.service';
import { ZodValidationPipe } from '../../common/pipes/zod-validation.pipe';
import { CurrentUser, RequiresCaptain } from '../../common/decorators/auth.decorators';

@Controller('saints')
export class SaintsController {
  constructor(private readonly saints: SaintsService) {}

  @Get()
  list(
    @Query(new ZodValidationPipe(listSaintsQuerySchema))
    query: ReturnType<typeof listSaintsQuerySchema.parse>,
  ) {
    return this.saints.list(query);
  }

  @Post()
  @RequiresCaptain()
  create(
    @CurrentUser() user: AuthUser,
    @Body(new ZodValidationPipe(createSaintSchema)) input: CreateSaintInput,
  ) {
    if (!user.cityId) throw new BadRequestException('No city scope');
    return this.saints.create(user.userId, input);
  }
}
