import {
  Body,
  Controller,
  Get,
  HttpStatus,
  Param,
  Post,
  Query,
  Req,
  Res,
  UseGuards,
  UsePipes,
} from '@nestjs/common';
import { Request, Response } from 'express';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { ZodValidationPipe } from '../../common/pipes/zod-validation.pipe';
import { JwtAuthGuard } from '../owner/guards/jwt.guard';
import {
  createProbationDto,
  createProbationSchema,
  filterProbationDto,
  filterProbationSchema,
  reviewProbationDto,
  reviewProbationSchema,
} from './probation.dto';
import { ProbationService } from './probation.service';

@Controller('probation')
export class ProbationController {
  constructor(private readonly service: ProbationService) {}

  @UseGuards(JwtAuthGuard)
  @UsePipes(new ZodValidationPipe(createProbationSchema))
  @Post()
  async createProbation(
    @Body() data: createProbationDto,
    @CurrentUser() owner_id: string,
    @Res() res: Response,
  ) {
    const result = await this.service.createProbation(data, owner_id);
    return res.status(HttpStatus.CREATED).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @UsePipes(new ZodValidationPipe(filterProbationSchema))
  @Get('filter')
  async getProbationsByFilter(
    @Query() query: filterProbationDto,
    @CurrentUser() owner_id: string,
    @Res() res: Response,
  ) {
    const result = await this.service.getProbationsByFilter(query, owner_id);
    return res.status(HttpStatus.OK).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @Get(':id')
  async getProbationById(
    @Param('id') id: string,
    @CurrentUser() owner_id: string,
    @Res() res: Response,
  ) {
    const result = await this.service.getProbationById(id, owner_id);
    return res.status(HttpStatus.OK).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @UsePipes(new ZodValidationPipe(reviewProbationSchema))
  @Post(':id/review')
  async reviewProbation(
    @Param('id') id: string,
    @Body() data: reviewProbationDto,
    @CurrentUser() owner_id: string,
    @Req() req: Request,
    @Res() res: Response,
  ) {
    const reviewerId = (req.user as any)?.sub || (req.user as any)?.id;
    const result = await this.service.reviewProbation(id, data, reviewerId, owner_id);
    return res.status(HttpStatus.OK).json(result);
  }
}
