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 {
  defaultPaginationDto,
  defaultPaginationSchema,
} from '../shared/shared.dto';
import {
  createDesignationDto,
  createDesignationSchema,
  updateDesignationDto,
  updateDesignationSchema,
} from './designations.dto';
import { DesignationsService } from './designations.service';

@Controller('designations')
export class DesignationsController {
  constructor(private readonly service: DesignationsService) {}

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

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

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

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

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

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