import { Body, Controller, Delete, Get, Inject, Param, Post, Put, Request, UseGuards } from '@nestjs/common';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
import { FileService } from '../file/file.service';

import { AuthGuard } from 'src/jwt-auth/auth.guard';
import { OptionalAuthGuard } from 'src/jwt-auth/optional-auth.guard';
import { CommercialPropertyListing } from '../models/db.model';
import { CommercialPropertyListingCriteria, JwtUser } from '../models/main.model';
import { CommercialPropertyService } from './commercial-property.service';

@Controller('listings/commercialProperty')
export class CommercialPropertyListingsController {
  constructor(
    private readonly listingsService: CommercialPropertyService,
    private fileService: FileService,
    @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
  ) { }

  @UseGuards(AuthGuard)
  @Post('favorites/all')
  async findFavorites(@Request() req): Promise<any> {
    return await this.listingsService.findFavoriteListings(req.user as JwtUser);
  }

  @UseGuards(OptionalAuthGuard)
  @Get(':slugOrId')
  async findById(@Request() req, @Param('slugOrId') slugOrId: string): Promise<any> {
    // Support both slug (e.g., "office-space-austin-tx-a3f7b2c1") and UUID
    return await this.listingsService.findCommercialBySlugOrId(slugOrId, req.user as JwtUser);
  }

  @UseGuards(OptionalAuthGuard)
  @Get('user/:email')
  async findByEmail(@Request() req, @Param('email') email: string): Promise<CommercialPropertyListing[]> {
    return await this.listingsService.findCommercialPropertiesByEmail(email, req.user as JwtUser);
  }

  @UseGuards(OptionalAuthGuard)
  @Post('find')
  async find(@Request() req, @Body() criteria: CommercialPropertyListingCriteria): Promise<any> {
    return await this.listingsService.searchCommercialProperties(criteria, req.user as JwtUser);
  }
  @UseGuards(OptionalAuthGuard)
  @Post('findTotal')
  async findTotal(@Request() req, @Body() criteria: CommercialPropertyListingCriteria): Promise<number> {
    return await this.listingsService.getCommercialPropertiesCount(criteria, req.user as JwtUser);
  }

  @UseGuards(OptionalAuthGuard)
  @Post()
  async create(@Body() listing: any) {
    return await this.listingsService.createListing(listing);
  }

  @UseGuards(OptionalAuthGuard)
  @Put()
  async update(@Request() req, @Body() listing: any) {
    return await this.listingsService.updateCommercialPropertyListing(listing.id, listing, req.user as JwtUser);
  }

  @UseGuards(OptionalAuthGuard)
  @Delete('listing/:id/:imagePath')
  async deleteById(@Param('id') id: string, @Param('imagePath') imagePath: string) {
    await this.listingsService.deleteListing(id);
    this.fileService.deleteDirectoryIfExists(imagePath);
  }

  @UseGuards(AuthGuard)
  @Post('favorite/:id')
  async addFavorite(@Request() req, @Param('id') id: string) {
    await this.listingsService.addFavorite(id, req.user as JwtUser);
    return { success: true, message: 'Added to favorites' };
  }

  @UseGuards(AuthGuard)
  @Delete('favorite/:id')
  async deleteFavorite(@Request() req, @Param('id') id: string) {
    await this.listingsService.deleteFavorite(id, req.user as JwtUser);
    return { success: true, message: 'Removed from favorites' };
  }
}
