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 {
  createHiringSourceDto,
  createHiringSourceSchema,
  updateHiringSourceDto,
  updateHiringSourceSchema,
} from './hiring_sources.dto';
import { HiringSourcesService } from './hiring_sources.service';

@Controller('hiring-sources')
export class HiringSourcesController {
  constructor(private readonly service: HiringSourcesService) {}

  @UseGuards(JwtAuthGuard)
  @UsePipes(new ZodValidationPipe(createHiringSourceSchema))
  @Post('create')
  async create(
    @Body() data: createHiringSourceDto,
    @CurrentUser() owner_id: string,
    @Req() req: Request,
    @Res() res: Response,
  ) {
    const result = await this.service.createHiringSource(
      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.getHiringSources(
      owner_id,
      getPermissionContext(req.user as any),
    );
    return res.status(HttpStatus.OK).json(result);
  }

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

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

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

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