import { Controller, Delete, Inject, Param, Post, UploadedFile, UseGuards, UseInterceptors } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { AuthGuard } from 'src/jwt-auth/auth.guard';
import { Logger } from 'winston';
import { FileService } from '../file/file.service';
import { CommercialPropertyService } from '../listings/commercial-property.service';
import { SelectOptionsService } from '../select-options/select-options.service';

@Controller('image')
export class ImageController {
  constructor(
    private fileService: FileService,
    private listingService: CommercialPropertyService,
    @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
    private selectOptions: SelectOptionsService,
  ) {}
  // ############
  // Property
  // ############
  @UseGuards(AuthGuard)
  @Post('uploadPropertyPicture/:imagePath/:serial')
  @UseInterceptors(FileInterceptor('file'))
  async uploadPropertyPicture(@UploadedFile() file: Express.Multer.File, @Param('imagePath') imagePath: string, @Param('serial') serial: string) {
    const imagename = await this.fileService.storePropertyPicture(file, imagePath, serial);
    await this.listingService.addImage(imagePath, serial, imagename);
  }
  @UseGuards(AuthGuard)
  @Delete('propertyPicture/:imagePath/:serial/:imagename')
  async deletePropertyImagesById(@Param('imagePath') imagePath: string, @Param('serial') serial: string, @Param('imagename') imagename: string): Promise<any> {
    this.fileService.deleteImage(`pictures/property/${imagePath}/${serial}/${imagename}`);
    await this.listingService.deleteImage(imagePath, serial, imagename);
  }
  // ############
  // Profile
  // ############
  @UseGuards(AuthGuard)
  @Post('uploadProfile/:email')
  @UseInterceptors(FileInterceptor('file'))
  async uploadProfile(@UploadedFile() file: Express.Multer.File, @Param('email') adjustedEmail: string) {
    await this.fileService.storeProfilePicture(file, adjustedEmail);
  }
  @UseGuards(AuthGuard)
  @Delete('profile/:email/')
  async deleteProfileImagesById(@Param('email') email: string): Promise<any> {
    this.fileService.deleteImage(`pictures/profile/${email}.avif`);
  }
  // ############
  // Logo
  // ############
  @UseGuards(AuthGuard)
  @Post('uploadCompanyLogo/:email')
  @UseInterceptors(FileInterceptor('file'))
  async uploadCompanyLogo(@UploadedFile() file: Express.Multer.File, @Param('email') adjustedEmail: string) {
    await this.fileService.storeCompanyLogo(file, adjustedEmail);
  }
  @UseGuards(AuthGuard)
  @Delete('logo/:email/')
  async deleteLogoImagesById(@Param('email') adjustedEmail: string): Promise<any> {
    this.fileService.deleteImage(`pictures/logo/${adjustedEmail}.avif`);
  }
}
