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 {
  createEmployementTypeDto,
  createEmployementTypeSchema,
  updateEmployementTypeDto,
  updateEmployementTypeSchema,
} from './employement_types.dto';
import { EmployementTypesService } from './employement_types.service';

@Controller('employement-types')
export class EmployementTypesController {
  constructor(private readonly service: EmployementTypesService) {}

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

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

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

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

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