import { Controller, Get, Inject, Param, Request, UseGuards } from '@nestjs/common';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { OptionalAuthGuard } from 'src/jwt-auth/optional-auth.guard';
import { Logger } from 'winston';
import { BusinessListingService } from './business-listing.service';
import { CommercialPropertyService } from './commercial-property.service';

@Controller('listings/undefined')
export class UnknownListingsController {
  constructor(
    @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
    private readonly businessListingsService: BusinessListingService,
    private readonly propertyListingsService: CommercialPropertyService,
  ) {}

  @UseGuards(OptionalAuthGuard)
  @Get(':id')
  async findById(@Request() req, @Param('id') id: string): Promise<any> {
    try {
      return await this.businessListingsService.findBusinessesById(id, req.user);
    } catch (error) {
      return await this.propertyListingsService.findCommercialPropertiesById(id, req.user);
    }
  }
}
