import {
  Body,
  Controller,
  Delete,
  Get,
  HttpStatus,
  Param,
  Patch,
  Post,
  Query,
  Res,
  UseGuards,
  UsePipes,
} from '@nestjs/common';
import { Response } from 'express';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { ZodValidationPipe } from '../../common/pipes/zod-validation.pipe';
import { JwtAuthGuard } from '../owner/guards/jwt.guard';
import {
  createEventDto,
  createEventSchema,
  eventFilterDto,
  eventFilterSchema,
  updateEventDto,
  updateEventSchema,
} from './events.dto';
import { EventsService } from './events.service';

@Controller('events')
export class EventsController {
  constructor(private readonly service: EventsService) {}

  @UseGuards(JwtAuthGuard)
  @UsePipes(new ZodValidationPipe(createEventSchema))
  @Post('create')
  async create(
    @Body() data: createEventDto,
    @CurrentUser() owner_id: string,
    @Res() res: Response,
  ) {
    const result = await this.service.createEvent(data, owner_id);
    return res.status(HttpStatus.CREATED).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @UsePipes(new ZodValidationPipe(eventFilterSchema))
  @Get('all')
  async getAll(
    @Query() query: eventFilterDto,
    @CurrentUser() owner_id: string,
    @Res() res: Response,
  ) {
    const result = await this.service.getEvents(query, owner_id);
    return res.status(HttpStatus.OK).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @Get('find/:eventId')
  async find(
    @Param('eventId') eventId: string,
    @CurrentUser() owner_id: string,
    @Res() res: Response,
  ) {
    const result = await this.service.getEventById(eventId, owner_id);
    return res.status(HttpStatus.OK).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @UsePipes(new ZodValidationPipe(updateEventSchema))
  @Patch('update/:eventId')
  async update(
    @Param('eventId') eventId: string,
    @CurrentUser() owner_id: string,
    @Body() data: updateEventDto,
    @Res() res: Response,
  ) {
    const result = await this.service.updateEvent(eventId, data, owner_id);
    return res.status(HttpStatus.OK).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @Delete('delete/:eventId')
  async delete(
    @Param('eventId') eventId: string,
    @CurrentUser() owner_id: string,
    @Res() res: Response,
  ) {
    const result = await this.service.deleteEvent(eventId, owner_id);
    return res.status(HttpStatus.OK).json(result);
  }
}
