import {
  Body,
  Controller,
  HttpStatus,
  Param,
  Patch,
  Post,
  Res,
  UseGuards,
  UsePipes,
} from '@nestjs/common';
import { Response } from 'express';
import { ZodValidationPipe } from '../../common/pipes/zod-validation.pipe';
import { logger } from '../../config';
import {
  changePasswordDto,
  changePasswordSchema,
  resetOtpDto,
  resetOtpSchema,
  resetPasswordDto,
  resetPasswordSchema,
  verifyOtpDto,
  verifyOtpSchema,
} from '../shared/shared.dto';
import { LoginThrottlerGuard } from './guards/login-throttle.guard';
import {
  createOwnerDto,
  createOwnerSchema,
  resendOtpDto,
  resendOtpSchema,
  signinOwnerDto,
  signinOwnerSchema,
  updateOwnerDto,
  updateOwnerSchema,
} from './owner.dto';
import { OwnerService } from './owner.service';

@Controller('owner')
export class OwnerController {
  constructor(private readonly service: OwnerService) {}

  @UsePipes(new ZodValidationPipe(createOwnerSchema))
  @Post('signup')
  async signup(@Body() data: createOwnerDto, @Res() res: Response) {
    const result = await this.service.createOwner(data);
    return res.status(HttpStatus.CREATED).json(result);
  }

  @UsePipes(new ZodValidationPipe(resendOtpSchema))
  @Post('resend-otp')
  async resendOtp(@Body() data: resendOtpDto, @Res() res: Response) {
    const result = await this.service.resendOtp(data?.email);
    return res.status(HttpStatus.OK).json(result);
  }

  @UsePipes(new ZodValidationPipe(verifyOtpSchema))
  @Post('verify-otp')
  async verifyOtp(@Body() data: verifyOtpDto, @Res() res: Response) {
    logger.info(`Received OTP verification request for email: ${data?.email}`);
    const result = await this.service.verifyOtp(data?.email, data?.otp);
    return res.status(HttpStatus.OK).json(result);
  }

  @UseGuards(LoginThrottlerGuard)
  @UsePipes(new ZodValidationPipe(signinOwnerSchema))
  @Post('signin')
  async singin(@Body() data: signinOwnerDto, @Res() res: Response) {
    const result = await this.service.signin(data);
    return res.status(HttpStatus.OK).json(result);
  }

  @UsePipes(new ZodValidationPipe(updateOwnerSchema))
  @Patch('update/:id')
  async update(
    @Param('id') id: string,
    @Body() data: updateOwnerDto,
    @Res() res: Response,
  ) {
    logger.info(
      `Received update request for owner ID: ${id} data ${JSON.stringify(data)}`,
    );
    const result = await this.service.updateOwner(data, id);
    return res.status(HttpStatus.OK).json(result);
  }

  @UsePipes(new ZodValidationPipe(resendOtpSchema))
  @Post('forgot-password')
  async forgotPassword(@Body() data: resendOtpDto, @Res() res: Response) {
    const result = await this.service.forgotPassword(data?.email);
    return res.status(HttpStatus.OK).json(result);
  }

  @UsePipes(new ZodValidationPipe(resetOtpSchema))
  @Post('reset-verify-otp')
  async resetVerifyOtp(@Body() data: resetOtpDto, @Res() res: Response) {
    const result = await this.service.resetVerifyOtp(data);
    return res.status(HttpStatus.OK).json(result);
  }

  @UsePipes(new ZodValidationPipe(resetPasswordSchema))
  @Post('reset-password')
  async resetPassword(@Body() data: resetPasswordDto, @Res() res: Response) {
    const result = await this.service.resetPassword(data);
    return res.status(HttpStatus.OK).json(result);
  }

  @UsePipes(new ZodValidationPipe(changePasswordSchema))
  @Patch('change-password/:id')
  async changePassword(
    @Param('id') id: string,
    @Body() data: changePasswordDto,
    @Res() res: Response,
  ) {
    const result = await this.service.changePassword(id, data);
    return res.status(HttpStatus.OK).json(result);
  }
}
