package com.example.backend.books.api;

import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.backend.categories.api.CategoryDto;
import com.example.backend.categories.model.CategoryEntity;
import com.example.backend.categories.service.CategoryService;

@Controller
@RequestMapping(CatalogMobileController.URL)
public class CatalogMobileController {
    public static final String URL = "/сatalog-mobile";

    private static final String CATALOG_MOBILE_VIEW = "catalog-mobile";

    private final CategoryService categoryService;
    private final ModelMapper modelMapper;

    public CatalogMobileController(
            CategoryService categoryService,
            ModelMapper modelMapper) {
        this.categoryService = categoryService;
        this.modelMapper = modelMapper;
    }

    private CategoryDto toCategoryDto(CategoryEntity entity) {
        return modelMapper.map(entity, CategoryDto.class);
    }

    @GetMapping
    public String getCatalogMobile(Model model) {

        model.addAttribute(
                "categories",
                categoryService.getAll().stream()
                        .map(this::toCategoryDto)
                        .toList());
        return CATALOG_MOBILE_VIEW;
    }
}
