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 { OptionalJwtAuthGuard } from '../jwt-auth/optional-jwt-auth.guard.js';
import { BusinessListingCriteria, JwtUser } from '../models/main.model.js';
import { BusinessListingService } from './business-listing.service.js';

@Controller('listings/business')
export class BusinessListingsController {
  constructor(
    private readonly listingsService: BusinessListingService,
    @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
  ) {}

  @UseGuards(OptionalJwtAuthGuard)
  @Get(':id')
  findById(@Request() req, @Param('id') id: string): any {
    return this.listingsService.findBusinessesById(id, req.user as JwtUser);
  }

  @UseGuards(OptionalJwtAuthGuard)
  @Get('user/:userid')
  findByUserId(@Request() req, @Param('userid') userid: string): any {
    return this.listingsService.findBusinessesByEmail(userid, req.user as JwtUser);
  }

  @UseGuards(OptionalJwtAuthGuard)
  @Post('find')
  find(@Request() req, @Body() criteria: BusinessListingCriteria): any {
    return this.listingsService.searchBusinessListings(criteria, req.user as JwtUser);
  }
  @Post('findTotal')
  findTotal(@Body() criteria: BusinessListingCriteria): Promise<number> {
    return this.listingsService.getBusinessListingsCount(criteria);
  }
  // @UseGuards(OptionalJwtAuthGuard)
  // @Post('search')
  // search(@Request() req, @Body() criteria: BusinessListingCriteria): any {
  //   return this.listingsService.searchBusinessListings(criteria.prompt);
  // }

  @Post()
  create(@Body() listing: any) {
    this.logger.info(`Save Listing`);
    return this.listingsService.createListing(listing);
  }
  @Put()
  update(@Body() listing: any) {
    this.logger.info(`Save Listing`);
    return this.listingsService.updateBusinessListing(listing.id, listing);
  }
  @Delete(':id')
  deleteById(@Param('id') id: string) {
    this.listingsService.deleteListing(id);
  }
  @Get('states/all')
  getStates(): any {
    return this.listingsService.getStates();
  }
}
