import { Controller, Get, Param, Query, Res } from '@nestjs/common';
import { ProductSelectionService } from './product-selection.service';

@Controller('product-selection')
export class ProductSelectionController {
  constructor(private readonly productSelectionService: ProductSelectionService) {}

  @Get('wheel-sizes')
  getWheelSizes(
    @Query('applicationId') applicationId = '',
    @Query('loadId') loadId = '',
  ) {
    return this.productSelectionService.getWheelSizes({
      applicationId,
      loadId,
    });
  }

  @Get('wheel-collections')
  getWheelCollections(
    @Query('applicationId') applicationId = '',
    @Query('loadId') loadId = '',
    @Query('wheelSizeId') wheelSizeId = '',
    @Query('wheelSizeCode') wheelSizeCode = '',
    @Query('wheelSizeInch') wheelSizeInch = '',
  ) {
    return this.productSelectionService.getWheelCollections({
      applicationId,
      loadId,
      wheelSizeId,
      wheelSizeCode,
      wheelSizeInch,
    });
  }

  @Get('suitable-wheels')
  getSuitableWheels(
    @Query('applicationId') applicationId = '',
    @Query('loadId') loadId = '',
    @Query('wheelSizeInch') wheelSizeInch = '',
    @Query('wheelTypeCode') wheelTypeCode = '',
  ) {
    return this.productSelectionService.getSuitableWheels({
      applicationId,
      loadId,
      wheelSizeInch,
      wheelTypeCode,
    });
  }

  @Get('mountings')
  getMountings(
    @Query('applicationId') applicationId = '',
    @Query('loadId') loadId = '',
    @Query('wheelSizeInch') wheelSizeInch = '',
    @Query('wheelTypeCode') wheelTypeCode = '',
    @Query('wheelTagCode') wheelTagCode = '',
  ) {
    return this.productSelectionService.getMountings({
      applicationId,
      loadId,
      wheelSizeInch,
      wheelTypeCode,
      wheelTagCode,
    });
  }

  @Get('plates')
  getPlates(
    @Query('applicationId') applicationId = '',
    @Query('loadId') loadId = '',
    @Query('wheelSizeInch') wheelSizeInch = '',
    @Query('wheelTypeCode') wheelTypeCode = '',
    @Query('wheelTagCode') wheelTagCode = '',
    @Query('mountCode') mountCode = '',
  ) {
    return this.productSelectionService.getPlates({
      applicationId,
      loadId,
      wheelSizeInch,
      wheelTypeCode,
      wheelTagCode,
      mountCode,
    });
  }

  @Get('products')
  getProducts(
    @Query('applicationId') applicationId = '',
    @Query('loadId') loadId = '',
    @Query('wheelSizeInch') wheelSizeInch = '',
    @Query('wheelTypeCode') wheelTypeCode = '',
    @Query('wheelTagCode') wheelTagCode = '',
    @Query('mountCode') mountCode = '',
    @Query('mountLoad') mountLoad = '',
  ) {
    return this.productSelectionService.getProducts({
      applicationId,
      loadId,
      wheelSizeInch,
      wheelTypeCode,
      wheelTagCode,
      mountCode,
      mountLoad,
    });
  }

  @Get('final-product-search')
  searchFinalProducts(
    @Query('search') search = '',
    @Query('pageSize') pageSize = '20',
  ) {
    return this.productSelectionService.searchFinalProducts(search, pageSize);
  }

  @Get('products/:code/brochure')
  async downloadProductBrochure(@Param('code') code = '', @Res() response: any) {
    const brochure = await this.productSelectionService.getProductBrochure(code);

    response.set({
      'Content-Type': 'application/pdf',
      'Content-Disposition': `attachment; filename="${brochure.filename}"`,
      'Content-Length': brochure.buffer.length,
    });
    response.end(brochure.buffer);
  }

  @Get('products/:code')
  getProductInformation(@Param('code') code = '') {
    return this.productSelectionService.getProductInformation(code);
  }
}
