import {
  BadRequestException,
  Body,
  Controller,
  Delete,
  Get,
  Post,
  Query,
} from '@nestjs/common';
import { FavoritesService } from './favorites.service';
import { ToggleFavoriteDto } from './dto/favorite.dto';

@Controller('api/v1/favorites')
export class FavoritesController {
  constructor(private readonly service: FavoritesService) {}

  @Post('toggle')
  async toggle(@Body() dto: ToggleFavoriteDto) {
    const data = await this.service.toggle(dto);
    return { success: true, message: data.favorited ? 'Added to favorites' : 'Removed from favorites', data };
  }

  @Get()
  async find(@Query('sessionId') sessionId: string, @Query('entityType') entityType?: string) {
    if (!sessionId) throw new BadRequestException('sessionId is required');
    const data = await this.service.findBySession(sessionId, entityType);
    return { success: true, message: 'Success', data };
  }

  @Delete()
  async remove(
    @Query('sessionId') sessionId: string,
    @Query('entityType') entityType: string,
    @Query('entityId') entityId: string,
  ) {
    if (!sessionId || !entityType || !entityId) {
      throw new BadRequestException('sessionId, entityType and entityId are required');
    }
    const success = await this.service.remove(sessionId, entityType, Number(entityId));
    return { success, message: success ? 'Removed from favorites' : 'Favorite not found' };
  }
}
