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;
import org.springframework.web.multipart.MultipartFile;

@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());
        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());
                model.addAttribute("currentThumbnailUrl", portfolio.getImageUrl());
                model.addAttribute("portfolioMeta", portfolio);
            }
        }

        model.addAttribute("form", form);
        return "kr/iotdoor/boot/admin/portfolio/form";
    }

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

        if (bindingResult.hasErrors()) {
            if (form.getThumbnailAtchFileIdntfNo() != null && !form.getThumbnailAtchFileIdntfNo().isBlank()) {
                model.addAttribute("currentThumbnailUrl", portfolioService.resolveThumbnailUrl(form.getThumbnailAtchFileIdntfNo(), 1));
            }
            return "kr/iotdoor/boot/admin/portfolio/form";
        }

        portfolioService.save(form, thumbnailFile, (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";
    }
}
