package kr.iotdoor.boot.web;

import kr.iotdoor.boot.service.PortfolioService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class PublicPageController {

    private final PortfolioService portfolioService;

    public PublicPageController(PortfolioService portfolioService) {
        this.portfolioService = portfolioService;
    }

    @GetMapping({"/", "/index.do"})
    public String home(Model model) {
        model.addAttribute("mainYn", "Y");
        return "kr/iotdoor/comn/site/mainPge";
    }

    @GetMapping("/contact.do")
    public String contact() {
        return "kr/iotdoor/cmty/contact/contact";
    }

    @GetMapping("/portfolio.do")
    public String portfolio(Model model) {
        model.addAttribute("portfolioItems", portfolioService.getPortfolioItems());
        return "kr/iotdoor/cmty/portfolio/portfolio";
    }

    @GetMapping("/portfolio/{portfolioId}")
    public String portfolioDetail(@org.springframework.web.bind.annotation.PathVariable String portfolioId, Model model) {
        var portfolio = portfolioService.getPortfolioDetail(portfolioId);
        if (portfolio == null) {
            throw new org.springframework.web.server.ResponseStatusException(
                org.springframework.http.HttpStatus.NOT_FOUND,
                "포트폴리오를 찾을 수 없습니다."
            );
        }

        model.addAttribute("portfolio", portfolio);
        return "kr/iotdoor/cmty/portfolio/detail";
    }
}
