package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Controller
public class HomeController {
    private final QuestionRepository questionRepository;

    @Autowired
    public HomeController(QuestionRepository questionRepository) {
        this.questionRepository = questionRepository;
    }

    @GetMapping(value = {"/", "/home"})
    public String home(@CookieValue(value = "crypto_question_session_token", required = false) String sessionToken, Model model) {
        if(sessionToken == null) {
            return "redirect:https://crypto-questions.caelus.ro/login";
        }
        List<Question> questions = this.questionRepository.findAll();

        model.addAttribute("questions", questions);

        return "index";
    }
}
