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;
import com.example.backend.core.utils.Formatter;

@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) {
        final BookDto dto = modelMapper.map(entity, BookDto.class);
        dto.setDate(Formatter.format(entity.getDate()));
        return dto;
    }

    @GetMapping
    public String getMainPage(Model model) {
        model.addAttribute(
                "novelties",
                bookService.getNewest(6));
        model.addAttribute(
                "promotions",
                bookService.getByIds(List.of(4L, 44L, 45L, 65L, 66L, 67L)).stream()
                        .map(this::toDto)
                        .toList());
        model.addAttribute(
                "recommendations",
                bookService.getByIds(List.of(68L, 69L, 70L, 71L, 72L, 75L)).stream()
                        .map(this::toDto)
                        .toList());
        return MAIN_VIEW;
    }
}
