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 {
  createDepartmentDto,
  createDepartmentSchema,
  updateDepartmentDto,
  updateDepartmentSchema,
} from './departments.dto';
import { DepartmentsService } from './departments.service';

@Controller('departments')
export class DepartmentsController {
  constructor(private readonly service: DepartmentsService) {}

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

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

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

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

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