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 {
  bulkUpdateLeaveStatusDto,
  bulkUpdateLeaveStatusSchema,
  createLeaveDto,
  createLeaveSchema,
  leaveFilterDto,
  leaveFilterSchema,
  updateLeaveDto,
  updateLeaveSchema,
} from './leaves.dto';
import { LeavesService } from './leaves.service';

@Controller('leaves')
export class LeavesController {
  constructor(private readonly service: LeavesService) {}

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

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

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

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

  @UseGuards(JwtAuthGuard)
  @UsePipes(new ZodValidationPipe(leaveFilterSchema))
  @Get('member/:memberId')
  async findByMember(
    @Param('memberId') memberId: string,
    @CurrentUser() owner_id: string,
    @Query() query: leaveFilterDto,
    @Req() req: Request,
    @Res() res: Response,
  ) {
    const result = await this.service.getLeaves(
      { ...query, member_id: memberId },
      owner_id,
      getPermissionContext(req.user as any),
    );
    return res.status(HttpStatus.OK).json(result);
  }

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

  @UseGuards(JwtAuthGuard)
  @UsePipes(new ZodValidationPipe(bulkUpdateLeaveStatusSchema))
  @Patch('update-bulk-status')
  async bulkUpdateStatus(
    @CurrentUser() owner_id: string,
    @Body() data: bulkUpdateLeaveStatusDto,
    @Req() req: Request,
    @Res() res: Response,
  ) {
    const result = await this.service.updateBulkLeaveStatus(
      data,
      owner_id,
      getPermissionContext(req.user as any),
    );
    return res.status(HttpStatus.OK).json(result);
  }

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