package fr.eni.enchere.controllers;

import fr.eni.enchere.bll.ArticleService;
import fr.eni.enchere.bll.CategorieService;
import fr.eni.enchere.bo.Article;
import fr.eni.enchere.bo.SearchArticleCritere;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.Locale;


@Controller
public class AccueilController {

    private static final Logger logger = LoggerFactory.getLogger(AccueilController.class);
    private ArticleService articleService;
    private CategorieService categorieService;


    public AccueilController(ArticleService articleService, CategorieService categorieService) {
        super();
        this.categorieService = categorieService;
        this.articleService = articleService;
    }

    @GetMapping({"/", "/accueil"})
    public String viewAccueil(@RequestParam(required = false) String searchTitle, @RequestParam(required = false) Integer searchCategory, Model model) {
        model.addAttribute("categories", categorieService.findAllCategories());
        SearchArticleCritere critere = new SearchArticleCritere();
        critere.setNoCategorie(searchCategory);
        critere.setTitle(searchTitle);

        model.addAttribute("articles", articleService.searchArticle(critere));
        return "accueil";
    }

    @PostMapping("/accueil")
    public String handleSearch(@RequestParam("searchTitle") String searchTitle, @RequestParam(value = "searchCategory", required = false) Integer searchCategory, Model model) {
        System.out.println("test");
        return viewAccueil(searchTitle, searchCategory, model);
    }


}
