package com.example.backend.books.api;

import java.util.List;

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

import com.example.backend.books.model.BookEntity;
import com.example.backend.books.service.BookService;

@Controller
public class MainPageController {
    private static final String MAIN_VIEW = "main-page";

    private final BookService bookService;
    private final ModelMapper modelMapper;

    public MainPageController(BookService bookService, ModelMapper modelMapper) {
        this.bookService = bookService;
        this.modelMapper = modelMapper;
    }

    private BookDto toDto(BookEntity entity) {
        return modelMapper.map(entity, BookDto.class);
    }

    @GetMapping
    public String getMainPage(Model model) {
        model.addAttribute(
                "novelties",
                bookService.getByIds(List.of(4L, 5L, 6L, 7L)).stream()
                        .map(this::toDto)
                        .toList());
        model.addAttribute(
                "promotions",
                bookService.getByIds(List.of(5L, 6L, 7L, 8L, 4L)).stream()
                        .map(this::toDto)
                        .toList());
        model.addAttribute(
                "recommendations",
                bookService.getByIds(List.of(6L, 4L, 8L, 5L, 7L)).stream()
                        .map(this::toDto)
                        .toList());
        return MAIN_VIEW;
    }
}
