import { Body, Controller, Get, Patch, Query, Req, UseGuards, UseInterceptors } from '@nestjs/common';
import { UserService } from './user.service';
import { UpdatePreferedCategoriesDto } from './dto/update-preferedCategories.dto';
import { CustomUserRequest } from 'src/auth/user/interface/customUserReq';
import { ApiBearerAuth, ApiBody, ApiConsumes, ApiQuery, ApiTags } from '@nestjs/swagger';
import { AuthUserGuard } from 'src/auth/user/authUser.guard';
import { LANGUAGES } from 'src/shared/enum/languages.enum';
import { UpdateImageUserDto, UpdateUserDto } from './dto/update-user.dto';
import { fileInterceptor } from 'src/shared/interceptors/file-save.interceptor';
import { CustomAdminRequest } from 'src/auth/admin/interface/customAdminReq';

@Controller('user')
@ApiTags('User')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Patch('update-prefered-categories')
  @ApiBearerAuth('jwt')
  @ApiBody({ type: UpdatePreferedCategoriesDto })
  @UseGuards(AuthUserGuard)
  async updatePreferedCategories(
    @Body() updatePreferedCategoriesDto: UpdatePreferedCategoriesDto,
    @Req() req: CustomUserRequest,
  ) {
    try {
      const { email } = req.user;
      await this.userService.updatePreferedCategories(email, updatePreferedCategoriesDto.idCategories);
      return { message: 'Prefered categories updated successfully' };
    } catch (error) {
      throw error;
    }
  }

  @Get('prefered-categories')
  @ApiBearerAuth('jwt')
  @ApiQuery({ name: 'lang', type: String })
  @UseGuards(AuthUserGuard)
  async getPreferedCategories(@Req() req: CustomUserRequest, @Query('lang') lang: string) {
    try {
      const { email } = req.user;
      return await this.userService.getPreferedCategories(email, lang as LANGUAGES);
    } catch (error) {
      throw error;
    }
  }

  @Patch('info')
  @ApiBearerAuth('jwt')
  @ApiBody({ type: UpdateUserDto })
  @UseGuards(AuthUserGuard)
  async updateInfo(@Body() updateUserDto: UpdateUserDto, @Req() req: CustomUserRequest) {
    try {
      const { email } = req.user;
      await this.userService.updateInfo(email, updateUserDto);
      return { message: 'User info updated successfully' };
    } catch (error) {
      throw error;
    }
  }

  @Patch('photo')
  @ApiBearerAuth('jwt')
  @UseGuards(AuthUserGuard)
  @ApiConsumes('multipart/form-data')
  @ApiBody({ type: UpdateImageUserDto })
  @UseInterceptors(fileInterceptor('image', 'static/user/', ['.jpg', '.jpeg', '.png']))
  async updatePhoto(@Req() reqImg: CustomAdminRequest, @Req() req: CustomUserRequest) {
    try {
      const { email } = req.user;
      const image = reqImg.file.filename;
      await this.userService.updatePhoto(email, image);
      return { message: 'User photo updated successfully' };
    } catch (error) {
      throw error;
    }
  }

  @Get('info')
  @ApiBearerAuth('jwt')
  @UseGuards(AuthUserGuard)
  async getInfo(@Req() req: CustomUserRequest) {
    try {
      const { email } = req.user;
      return await this.userService.getUserInfo(email);
    } catch (error) {
      throw error;
    }
  }
}
