package kr.iotdoor.boot.web;

import jakarta.validation.Valid;
import kr.iotdoor.boot.model.portfolio.PortfolioForm;
import kr.iotdoor.boot.model.portfolio.PortfolioItem;
import kr.iotdoor.boot.model.user.AdminUserPrincipal;
import kr.iotdoor.boot.service.PortfolioService;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
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;

@Controller
public class AdminPortfolioController {

    private final PortfolioService portfolioService;

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

    @GetMapping("/mfuc/portfolio/list.do")
    public String portfolioList(Model model) {
        model.addAttribute("portfolioItems", portfolioService.getAdminPortfolioItems());
        model.addAttribute("adminMenu", "portfolio");
        model.addAttribute("pageTitle", "포트폴리오 관리");
        model.addAttribute("pageDescription", "포트폴리오 콘텐츠를 작성하고 대표 이미지를 에디터 안에서 지정합니다.");
        return "kr/iotdoor/boot/admin/portfolio/list";
    }

    @GetMapping("/mfuc/portfolio/form.do")
    public String portfolioForm(
        @RequestParam(name = "portfolioIdntfNo", required = false) String portfolioIdntfNo,
        Model model
    ) {
        PortfolioForm form = new PortfolioForm();
        if (portfolioIdntfNo != null && !portfolioIdntfNo.isBlank()) {
            PortfolioItem portfolio = portfolioService.getPortfolioForEdit(portfolioIdntfNo);
            if (portfolio != null) {
                form.setPortfolioIdntfNo(portfolio.getPortfolioIdntfNo());
                form.setTitle(portfolio.getTitle());
                form.setContent(portfolio.getContent());
                form.setThumbnailAtchFileIdntfNo(portfolio.getThumbnailAtchFileIdntfNo());
                form.setThumbnailFileSn(portfolio.getThumbnailFileSn());
                model.addAttribute("currentThumbnailUrl", portfolio.getImageUrl());
            }
        }

        model.addAttribute("form", form);
        model.addAttribute("adminMenu", "portfolio");
        model.addAttribute("pageTitle", form.getPortfolioIdntfNo() != null && !form.getPortfolioIdntfNo().isBlank() ? "포트폴리오 수정" : "포트폴리오 등록");
        model.addAttribute("pageDescription", "Tiptap 에디터에서 본문 이미지를 업로드하고 대표 이미지를 지정할 수 있습니다.");
        return "kr/iotdoor/boot/admin/portfolio/form";
    }

    @PostMapping("/mfuc/portfolio/save.do")
    public String savePortfolio(
        @Valid @ModelAttribute("form") PortfolioForm form,
        BindingResult bindingResult,
        Authentication authentication,
        Model model
    ) {
        if ((form.getThumbnailAtchFileIdntfNo() == null || form.getThumbnailAtchFileIdntfNo().isBlank()) || form.getThumbnailFileSn() <= 0) {
            bindingResult.rejectValue("thumbnailAtchFileIdntfNo", "required", "대표 이미지는 필수입니다.");
        }

        if (bindingResult.hasErrors()) {
            if (form.getThumbnailAtchFileIdntfNo() != null && !form.getThumbnailAtchFileIdntfNo().isBlank() && form.getThumbnailFileSn() > 0) {
                model.addAttribute("currentThumbnailUrl", portfolioService.resolveThumbnailUrl(form.getThumbnailAtchFileIdntfNo(), form.getThumbnailFileSn()));
            }
            model.addAttribute("adminMenu", "portfolio");
            model.addAttribute("pageTitle", form.getPortfolioIdntfNo() != null && !form.getPortfolioIdntfNo().isBlank() ? "포트폴리오 수정" : "포트폴리오 등록");
            model.addAttribute("pageDescription", "Tiptap 에디터에서 본문 이미지를 업로드하고 대표 이미지를 지정할 수 있습니다.");
            return "kr/iotdoor/boot/admin/portfolio/form";
        }

        portfolioService.save(form, (AdminUserPrincipal) authentication.getPrincipal());
        return "redirect:/mfuc/portfolio/list.do?saved=1";
    }

    @PostMapping("/mfuc/portfolio/delete.do")
    public String deletePortfolio(@RequestParam("portfolioIdntfNo") String portfolioIdntfNo) {
        portfolioService.delete(portfolioIdntfNo);
        return "redirect:/mfuc/portfolio/list.do?deleted=1";
    }
}
