import {
  Controller,
  Get,
  HttpStatus,
  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 { monthHistoryQueryDto, monthHistoryQuerySchema } from './calendar.dto';
import { CalendarService } from './calendar.service';

@Controller('calendar')
export class CalendarController {
  constructor(private readonly service: CalendarService) {}

  @UseGuards(JwtAuthGuard)
  @UsePipes(new ZodValidationPipe(monthHistoryQuerySchema))
  @Get('month-history')
  async getMonthHistory(
    @CurrentUser() owner_id: string,
    @Query() query: monthHistoryQueryDto,
    @Res() res: Response,
    @Req() req: Request,
  ) {
    const result = await this.service.getMonthHistory(
      owner_id,
      getPermissionContext(req.user as any),
      query.month,
      query.year,
      query.data_scope,
    );
    return res.status(HttpStatus.OK).json(result);
  }
}
