package fr.eni.enchere.controllers;

import fr.eni.enchere.bll.ArticleService;
import fr.eni.enchere.bll.CategorieService;
import fr.eni.enchere.bll.UserService;
import fr.eni.enchere.bo.Article;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller()
@RequestMapping("/article")
public class ArticleController {

    private final ArticleService articleService;
    private final UserService userService;
    private CategorieService categorieService;

    public ArticleController(ArticleService articleService, UserService userService, CategorieService categorieService) {
        this.articleService = articleService;
        this.userService = userService;
        this.categorieService = categorieService;
    }

    @GetMapping
    public String viewArticle(Model model) {
        return "accueil";
    }

    //Affichage d'un article
    @GetMapping("/show")
    public String showArticle(@RequestParam(name = "slug")int id, Model model) {
        Article article = articleService.findArticleById(id);
        return "article";
    }

    @GetMapping("/{slug}")
    public String testShowArticle(@PathVariable(name = "slug")int id, Model model) {
        Article article = articleService.findArticleById(id);
        model.addAttribute("article", article);
        return "article";
    }

    @GetMapping("/new")
    public String test(@PathVariable(name = "slug")int id, Model model) {
        model.addAttribute("categories", categorieService.findAllCategories());
        return "newArticle";
    }

    @PostMapping("/new/add")
    public String newArticle(@ModelAttribute("article") Article article) {
        articleService.saveArticle(article);
        return "redirect:/accueil";
    }

    @PostMapping("/update")
    public String updateArticle() {
        return "redirect:/accueil";
    }

    @PostMapping("/delete")
    public String deleteArticle() {
        return "redirect:/accueil";
    }

}
