import {
  Body,
  Controller,
  Delete,
  Get,
  HttpStatus,
  Param,
  Patch,
  Post,
  Query,
  Req,
  Res,
  UseGuards,
  UsePipes,
} from '@nestjs/common';
import { Request, Response } from 'express';
import { getPermissionContext } from '../../common/access-control/permission-request.util';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { ZodValidationPipe } from '../../common/pipes/zod-validation.pipe';
import { JwtAuthGuard } from '../owner/guards/jwt.guard';
import {
  createHolidayDto,
  createHolidaySchema,
  holidayFilterDto,
  holidayFilterSchema,
  updateHolidayDto,
  updateHolidaySchema,
} from './holidays.dto';
import { HolidaysService } from './holidays.service';

@Controller('holidays')
export class HolidaysController {
  constructor(private readonly service: HolidaysService) {}

  @UseGuards(JwtAuthGuard)
  @UsePipes(new ZodValidationPipe(createHolidaySchema))
  @Post('create')
  async create(
    @Body() data: createHolidayDto,
    @CurrentUser() owner_id: string,
    @Req() req: Request,
    @Res() res: Response,
  ) {
    const result = await this.service.createHoliday(
      data,
      owner_id,
      getPermissionContext(req.user as any),
    );
    return res.status(HttpStatus.CREATED).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @UsePipes(new ZodValidationPipe(holidayFilterSchema))
  @Get('filter')
  async getAll(
    @Query() query: holidayFilterDto,
    @CurrentUser() owner_id: string,
    @Req() req: Request,
    @Res() res: Response,
  ) {
    const result = await this.service.getHolidays(
      query,
      owner_id,
      getPermissionContext(req.user as any),
    );
    return res.status(HttpStatus.OK).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @Get('find/:holidayId')
  async find(
    @Param('holidayId') holidayId: string,
    @CurrentUser() owner_id: string,
    @Req() req: Request,
    @Res() res: Response,
  ) {
    const result = await this.service.getHolidayById(
      holidayId,
      owner_id,
      getPermissionContext(req.user as any),
    );
    return res.status(HttpStatus.OK).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @UsePipes(new ZodValidationPipe(updateHolidaySchema))
  @Patch('update/:holidayId')
  async update(
    @Param('holidayId') holidayId: string,
    @CurrentUser() owner_id: string,
    @Body() data: updateHolidayDto,
    @Req() req: Request,
    @Res() res: Response,
  ) {
    const result = await this.service.updateHoliday(
      holidayId,
      data,
      owner_id,
      getPermissionContext(req.user as any),
    );
    return res.status(HttpStatus.OK).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @Delete('delete/:holidayId')
  async delete(
    @Param('holidayId') holidayId: string,
    @CurrentUser() owner_id: string,
    @Req() req: Request,
    @Res() res: Response,
  ) {
    const result = await this.service.deleteHoliday(
      holidayId,
      owner_id,
      getPermissionContext(req.user as any),
    );
    return res.status(HttpStatus.OK).json(result);
  }
}
