import {
  Controller,
  Get,
  HttpStatus,
  Query,
  Req,
  Res,
  UseGuards,
} 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 { JwtAuthGuard } from '../owner/guards/jwt.guard';
import { DashboardService } from './dashboard.service';

@Controller('dashboard')
export class DashboardController {
  constructor(private readonly service: DashboardService) {}

  @UseGuards(JwtAuthGuard)
  @Get('summary')
  async getDashboardSummery(
    @CurrentUser() owner_id: string,
    @Query('data_scope') dataScope: 'own' | 'all' | undefined,
    @Res() res: Response,
    @Req() req: Request,
  ) {
    const result = await this.service.getSummary(
      owner_id,
      getPermissionContext(req.user as any),
      dataScope,
    );
    return res.status(HttpStatus.OK).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @Get('this-week-summary')
  async getThisWeekSummary(
    @CurrentUser() owner_id: string,
    @Query('data_scope') dataScope: 'own' | 'all' | undefined,
    @Res() res: Response,
    @Req() req: Request,
  ) {
    const result = await this.service.getThisWeeks(
      owner_id,
      getPermissionContext(req.user as any),
      dataScope,
    );
    return res.status(HttpStatus.OK).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @Get('this-month-summary')
  async getThisMonthSummary(
    @CurrentUser() owner_id: string,
    @Query('data_scope') dataScope: 'own' | 'all' | undefined,
    @Res() res: Response,
    @Req() req: Request,
  ) {
    const result = await this.service.getThisMonthReport(
      owner_id,
      getPermissionContext(req.user as any),
      dataScope,
    );
    return res.status(HttpStatus.OK).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @Get('pending-leaves')
  async getPendingLeaves(
    @CurrentUser() owner_id: string,
    @Query('data_scope') dataScope: 'own' | 'all' | undefined,
    @Query('page') page: string | undefined,
    @Query('limit') limit: string | undefined,
    @Res() res: Response,
    @Req() req: Request,
  ) {
    const result = await this.service.getPendingLeaves(
      owner_id,
      getPermissionContext(req.user as any),
      {
        data_scope: dataScope,
        page: page ? Number(page) : undefined,
        limit: limit ? Number(limit) : undefined,
      },
    );
    return res.status(HttpStatus.OK).json(result);
  }
}
