import { Router } from "express";
import { asyncHandler } from "../../utils/asyncHandler.js";
import { RecipeRepository } from "../../repositories/RecipeRepository.js";
import { CompactRecipeHandler } from "../../handlers/CompactRecipeHandler.js";
import { CompactRecipeDtoEntityMapper } from "../../mappers/CompactRecipeDtoEntityMapper.js";
import {HttpStatusCode} from "../apiHelpers/HttpStatusCodes.js";

export const compactRecipeBasicRoute = "/compact-recipe"
/**
 * Handles all recipe related routes
 */
const router = Router();

// Inject repo + mapper here
const compactRecipeHandler = new CompactRecipeHandler(new RecipeRepository(), new CompactRecipeDtoEntityMapper());
/**
 * Load header data of all recipes
 * Responds with a list of CompactRecipeDtos
 * @todo request wrapper DTO
 * @todo response wrapper DTO
 */
router.get(
  "/",
  asyncHandler(async (req, res) => {  
    // extract search string from query parameters, convert to lower case for case insensitive search
    const searchString : string  = req.query.search ? req.query.search.toString().toLowerCase() : "";
    console.log("Searching for recipes with title containing", searchString)
    const response = await compactRecipeHandler.getMatchingRecipes(searchString);
    res.status(HttpStatusCode.OK).json(response);
  })
);

export default router;