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 {
  createNoticeDto,
  createNoticeSchema,
  noticeFilterDto,
  noticeFilterSchema,
  updateNoticeAcknowledgmentDto,
  UpdateNoticeAcknowledgmentSchema,
  updateNoticeDto,
  updateNoticeSchema,
} from './notice.dto';
import { NoticeService } from './notice.service';

@Controller('notice')
export class NoticeController {
  constructor(private readonly service: NoticeService) {}

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

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

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

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

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

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