package com.nailart.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nailart.dataobj.PortfolioDO;
import com.nailart.enums.RespCodeEnum;
import com.nailart.service.PortfolioService;
import com.nailart.vo.RespVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname PortfolioController
 * @Description 作品集控制类
 * @Date 2025/7/10 18:48
 * @Created by 21616
 */
@RestController
@RequestMapping("/portfolio")
public class PortfolioController {
    private final PortfolioService portfolioService;

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

    /**
     * 获取作品集列表
     *
     * @param page 页码
     * @param size 每页数量
     * @return RespVO
     */
    @GetMapping("/list")
    public RespVO getPortfolioList(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        RespVO respVO = new RespVO();
        if (page <= 0 || size <= 0) {
            respVO.setCode(RespCodeEnum.BAD_REQUEST.getCode());
            respVO.setMsg(RespCodeEnum.BAD_REQUEST.getMessage());
            return respVO;
        }
        try {
            IPage<PortfolioDO> portfolioDOIPage = portfolioService.listByPage(page, size);
            respVO.setCode(RespCodeEnum.SUCCESS.getCode());
            respVO.setMsg(RespCodeEnum.SUCCESS.getMessage());
            respVO.setData(portfolioDOIPage);
        } catch (Exception e) {
            respVO.setCode(RespCodeEnum.INTERNAL_SERVER_ERROR.getCode());
            respVO.setMsg(RespCodeEnum.INTERNAL_SERVER_ERROR.getMessage());
        }
        return respVO;

    }

    /**
     * 添加作品集
     *
     * @param portfolioDO 作品集信息
     * @return RespVO
     */
    @PostMapping("/add")
    public RespVO addPortfolio(@RequestBody PortfolioDO portfolioDO) {
        RespVO respVO = new RespVO();
        if (portfolioDO == null) {
            respVO.setCode(RespCodeEnum.BAD_REQUEST.getCode());
            respVO.setMsg(RespCodeEnum.BAD_REQUEST.getMessage());
            return respVO;
        }
        try {
            Integer i = portfolioService.addPortfolio(portfolioDO);
            respVO.setCode(RespCodeEnum.SUCCESS.getCode());
            respVO.setMsg(RespCodeEnum.SUCCESS.getMessage());
            respVO.setData(i);
        } catch (Exception e) {
            respVO.setCode(RespCodeEnum.INTERNAL_SERVER_ERROR.getCode());
            respVO.setMsg(RespCodeEnum.INTERNAL_SERVER_ERROR.getMessage());
        }
        return respVO;


    }

    /**
     * 更新作品集
     *
     * @param portfolioDO 作品集信息
     * @return RespVO
     */
    @PostMapping("/update")
    public RespVO updatePortfolio(@RequestBody PortfolioDO portfolioDO) {
        RespVO respVO = new RespVO();
        if (portfolioDO == null) {
            respVO.setCode(RespCodeEnum.BAD_REQUEST.getCode());
            respVO.setMsg(RespCodeEnum.BAD_REQUEST.getMessage());
            return respVO;
        }
        try {
            Integer i = portfolioService.updatePortfolio(portfolioDO);
            respVO.setCode(RespCodeEnum.SUCCESS.getCode());
            respVO.setMsg(RespCodeEnum.SUCCESS.getMessage());
            respVO.setData(i);
        } catch (Exception e) {
            respVO.setCode(RespCodeEnum.INTERNAL_SERVER_ERROR.getCode());
            respVO.setMsg(RespCodeEnum.INTERNAL_SERVER_ERROR.getMessage());
        }
        return respVO;
    }

    /**
     * 删除作品集
     *
     * @param id 作品集id
     * @return RespVO
     */
    @PostMapping("/delete")
    public RespVO deletePortfolio(@RequestParam("id") Integer id) {
        RespVO respVO = new RespVO();
        if (id == null) {
            respVO.setCode(RespCodeEnum.BAD_REQUEST.getCode());
            respVO.setMsg(RespCodeEnum.BAD_REQUEST.getMessage());
            return respVO;
        }
        try {
            Integer i = portfolioService.deletePortfolio(id);
            respVO.setCode(RespCodeEnum.SUCCESS.getCode());
            respVO.setMsg(RespCodeEnum.SUCCESS.getMessage());
            respVO.setData(i);
        } catch (Exception e) {
            respVO.setCode(RespCodeEnum.INTERNAL_SERVER_ERROR.getCode());
            respVO.setMsg(RespCodeEnum.INTERNAL_SERVER_ERROR.getMessage());
        }
        return respVO;
    }

    /**
     * 添加作品集
     *
     * @param id      作品集id
     * @param changes 点赞数
     * @return RespVO
     */
    @PostMapping("/addLoves")
    public RespVO addLoves(@RequestParam("id") Integer id, @RequestParam Integer changes) {
        RespVO respVO = new RespVO();
        if (id == null) {
            respVO.setCode(RespCodeEnum.BAD_REQUEST.getCode());
            respVO.setMsg(RespCodeEnum.BAD_REQUEST.getMessage());
            return respVO;
        }
        try {
            Integer i = portfolioService.addLoves(id, changes);
            respVO.setCode(RespCodeEnum.SUCCESS.getCode());
            respVO.setMsg(RespCodeEnum.SUCCESS.getMessage());
            respVO.setData(i);
        } catch (Exception e) {
            respVO.setCode(RespCodeEnum.INTERNAL_SERVER_ERROR.getCode());
            respVO.setMsg(RespCodeEnum.INTERNAL_SERVER_ERROR.getMessage());
        }
        return respVO;
    }

    @GetMapping("/getPortfolioByTag")
    public RespVO getPortfolioByTag(@RequestParam("tagID") Long tagID,
                                    @RequestParam("page") Integer page,
                                    @RequestParam("size") Integer size) {
        RespVO respVO = new RespVO();
        if (tagID == null) {
            respVO.setCode(RespCodeEnum.BAD_REQUEST.getCode());
            respVO.setMsg(RespCodeEnum.BAD_REQUEST.getMessage());
            return respVO;
        }
        try {
            IPage<PortfolioDO> portfolioByTag = portfolioService.getPortfolioByTag(tagID, page, size);
            respVO.setCode(RespCodeEnum.SUCCESS.getCode());
            respVO.setMsg(RespCodeEnum.SUCCESS.getMessage());
            respVO.setData(portfolioByTag);
        } catch (Exception e) {
            respVO.setCode(RespCodeEnum.INTERNAL_SERVER_ERROR.getCode());
            respVO.setMsg(RespCodeEnum.INTERNAL_SERVER_ERROR.getMessage());
        }
        return respVO;
    }


}
