import { Controller, Get, Query, Param, Post, Body, HttpCode, HttpStatus } from '@nestjs/common';
import { CatalogService } from './catalog.service';
import { CatalogResponseDto } from './dto/catalog-response.dto';
import { CatalogFiltersDto } from './dto/catalog-filters.dto';
import {
  CatalogItemShortDto,
  CatalogItemLongDto,
} from './dto/catalog-item.dto';
import {
  ApiQuery,
  ApiResponse,
  ApiOperation,
  ApiProperty,
  ApiBody,
  ApiTags,
} from '@nestjs/swagger';
import { MainPageCatalogResponseDto } from './dto/main-page-catalog-response.dto';
import { CreateYachtDto } from './dto/create-yacht.dto';

@ApiTags('catalog')
@Controller('catalog')
export class CatalogController {
  constructor(private readonly catalogService: CatalogService) {}

  @Get('filter')
  @ApiOperation({ summary: 'Filter catalog items with query parameters' })
  @ApiResponse({
    status: 200,
    description: 'Filtered catalog items',
    type: CatalogResponseDto,
  })
  @ApiQuery({ name: 'search', required: false, type: String })
  @ApiQuery({ name: 'minLength', required: false, type: Number })
  @ApiQuery({ name: 'maxLength', required: false, type: Number })
  @ApiQuery({ name: 'minPrice', required: false, type: Number })
  @ApiQuery({ name: 'maxPrice', required: false, type: Number })
  @ApiQuery({ name: 'minYear', required: false, type: Number })
  @ApiQuery({ name: 'maxYear', required: false, type: Number })
  @ApiQuery({ name: 'guests', required: false, type: Number })
  @ApiQuery({ name: 'sortByPrice', required: false, type: String })
  @ApiQuery({ name: 'paymentType', required: false, type: String })
  @ApiQuery({ name: 'quickBooking', required: false, type: Boolean })
  @ApiQuery({ name: 'hasToilet', required: false, type: Boolean })
  @ApiQuery({ name: 'date', required: false, type: Date })
  @ApiQuery({ name: 'departureTime', required: false, type: String })
  @ApiQuery({ name: 'arrivalTime', required: false, type: String })
  async getFilteredCatalog(
    @Query() filters: CatalogFiltersDto,
  ): Promise<CatalogResponseDto> {
    return this.catalogService.getCatalog(filters);
  }

  @Get()
  @ApiOperation({ summary: 'Get all catalog items (deprecated, use /filter)' })
  @ApiResponse({
    status: 200,
    description: 'All catalog items',
    type: [CatalogItemShortDto],
  })
  async getAllCatalogItems(): Promise<CatalogItemShortDto[]> {
    return this.catalogService.getAllCatalogItems();
  }

  @Get('main-page')
  @ApiOperation({ summary: 'Get catalog for main page' })
  @ApiProperty({ type: MainPageCatalogResponseDto })
  async getMainPageCatalog(): Promise<MainPageCatalogResponseDto | null> {
    return this.catalogService.getMainPageCatalog();
  }

  @Get('user/:userId')
  @ApiOperation({ summary: 'Get catalog items by user ID' })
  @ApiResponse({
    status: 200,
    description: 'Catalog items for the specified user',
    type: [CatalogItemShortDto],
  })
  async getCatalogByUserId(
    @Param('userId') userId: string,
  ): Promise<CatalogItemShortDto[]> {
    return this.catalogService.getCatalogByUserId(Number(userId));
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get catalog item by ID with full details' })
  @ApiResponse({
    status: 200,
    description: 'Catalog item with reviews, reservations and owner',
    type: CatalogItemLongDto,
  })
  async getCatalogItemById(
    @Param('id') id: string,
  ): Promise<CatalogItemLongDto | null> {
    return this.catalogService.getCatalogItemById(Number(id));
  }

  @Post()
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({ summary: 'Create a new yacht in catalog' })
  @ApiBody({ type: CreateYachtDto })
  @ApiResponse({
    status: 201,
    description: 'Yacht successfully created',
    type: CatalogItemLongDto,
  })
  @ApiResponse({
    status: 400,
    description: 'Bad request',
  })
  async createYacht(@Body() createYachtDto: CreateYachtDto): Promise<CatalogItemLongDto> {
    return this.catalogService.createYacht(createYachtDto);
  }
}
