import {
  Body,
  Controller,
  Get,
  Patch,
  Post,
  Query,
  Req,
  UploadedFile,
  UseGuards,
  UseInterceptors,
} from "@nestjs/common"
import { FileInterceptor } from "@nestjs/platform-express"
import { ApiBearerAuth, ApiBody, ApiConsumes, ApiTags } from "@nestjs/swagger"
import { Request } from "express"
import { profilePicConfig } from "../../common/file-upload/profile-pic.config"
import { AuthGuardMiddleware } from "../../middleware/auth-guard.middleware"
import { AuthService } from "./auth.service"
import { ChangePasswordDto } from "./dto/change-password.dto"
import { EditProfileDto } from "./dto/edit-profile.dto"
import { LoginDto } from "./dto/login.dto"
import { ResetPasswordDto } from "./dto/reset-password.dto"
import { SetPasswordDto } from "./dto/set-password.dto"
import { RequestDemoDto } from "./dto/request-demo.dto"

@Controller("auth")
@ApiTags("Auth")
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Post("login")
  login(@Body() loginDto: LoginDto) {
    return this.authService.login(loginDto)
  }

  @Post("forgot-password")
  @ApiBody({
    schema: {
      type: "object",
      properties: {
        email: {
          type: "string",
          example: "admin@admin.com",
          description: "Your registered email address",
        },
      },
      required: ["name"],
    },
  })
  forgotPassword(@Body("email") email: string) {
    return this.authService.forgotPassword(email)
  }

  @Post("reset-password")
  @ApiBody({
    description: "Reset Password",
    type: ResetPasswordDto,
  })
  resetPassword(@Body() resetPasswordDto: ResetPasswordDto) {
    return this.authService.resetPassword(resetPasswordDto)
  }

  @Post("set-password")
  setPassword(@Body() setPasswordDto: SetPasswordDto) {
    return this.authService.setPassword(setPasswordDto)
  }

  @Get("get-profile")
  @ApiBearerAuth("access-token")
  @UseGuards(AuthGuardMiddleware)
  getProfile(@Req() request: Request) {
    return this.authService.getProfile(request.headers["authorization"])
  }

  @Patch("edit-profile")
  @ApiConsumes("multipart/form-data")
  @ApiBody({
    description: "Edit profile",
    schema: {
      type: "object",
      properties: {
        first_name: { type: "string", description: "First name of the user" },
        last_name: {
          type: "string",
          description: "Last name of the user",
          nullable: true,
        },
        contact_no: {
          type: "string",
          description: "Mobile number of the user",
        },
        profile_pic: {
          type: "string",
          format: "binary",
          description: "Profile picture of the user",
        },
      },
      required: ["first_name", "contact_no"],
    },
  })
  @ApiBearerAuth("access-token")
  @UseGuards(AuthGuardMiddleware)
  @UseInterceptors(FileInterceptor("profile_pic", profilePicConfig))
  editProfile(
    @Req() request: Request,
    @Body() editProfileDto: EditProfileDto,
    @UploadedFile() profile_pic: any,
  ) {
    return this.authService.editProfile(
      request.headers["authorization"],
      editProfileDto,
      profile_pic,
    )
  }

  @Post("change-password")
  @ApiBearerAuth("access-token")
  @UseGuards(AuthGuardMiddleware)
  changePassword(
    @Req() request: Request,
    @Body() changePasswordDto: ChangePasswordDto,
  ) {
    return this.authService.changePassword(
      request.headers["authorization"],
      changePasswordDto,
    )
  }

  @Post("logout")
  @ApiBearerAuth("access-token")
  @UseGuards(AuthGuardMiddleware)
  logout(@Req() request: Request) {
    return this.authService.logout(request.headers["authorization"])
  }

  @Post("refresh-token")
  regenerateRefreshToken(@Req() request: Request) {
    return this.authService.regenerateRefreshToken(
      request.headers["authorization"],
    )
  }

  @Post("request-demo")
  requestDemo(@Body() requestDemoDto: RequestDemoDto) {
    return this.authService.requestDemo(requestDemoDto)
  }

  @Get("verify-email")
  verifyEmail(@Query("token") token: string) {
    return this.authService.verifyEmail(token)
  }

  @Post("bulk-send-feature-emails")
  @UseInterceptors(FileInterceptor("file"))
  @ApiConsumes("multipart/form-data")
  @ApiBody({
    description:
      "Excel file with name and email columns for bulk email sending",
    schema: {
      type: "object",
      properties: {
        file: {
          type: "string",
          format: "binary",
          description: "Excel file (.xlsx) containing name and email columns",
        },
      },
      required: ["file"],
    },
  })
  bulkSendFeatureEmails(@UploadedFile() file: Express.Multer.File) {
    if (!file) {
      return {
        success: false,
        message: "Excel file is required",
      }
    }

    return this.authService.bulkSendFeatureEmails(file.buffer)
  }
}
