package com.mvrent.carrental.controller;

import com.mvrent.carrental.model.Car;
import com.mvrent.carrental.service.CarService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;

@Controller
@RequiredArgsConstructor
public class HomeController {

    private final CarService carService;

    @GetMapping("/")
    public String home(Model model) {
        List<Car> available = carService.findAvailable();
        BigDecimal minRate = available.stream()
                .map(Car::getPricePerDay)
                .min(Comparator.naturalOrder())
                .orElse(BigDecimal.valueOf(800));
        model.addAttribute("featuredCars", available.stream().limit(8).toList());
        model.addAttribute("minRate", minRate);
        return "index";
    }
}
