package kr.iotdoor.boot.mapper;

import kr.iotdoor.boot.model.portfolio.PortfolioItem;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface PortfolioMapper {

    @Select("""
        SELECT
            p.PORTFOLIO_IDNTF_NO,
            p.TITLE,
            p.CONTENT,
            p.THUMBNAIL_ATCH_FILE_IDNTF_NO,
            p.THUMBNAIL_FILE_SN,
            p.REG_DT AS REGISTERED_AT,
            p.INQ_CNT AS VIEW_COUNT,
            p.REG_USER_IDNTF_NO AS REGISTERED_BY_USER_IDNTF_NO,
            u.USER_NM AS REGISTERED_BY_NAME,
            p.DEL_YN,
            p.UPD_DT AS UPDATED_AT
        FROM TN_PORTFOLIO p
        JOIN TN_USER u ON u.USER_IDNTF_NO = p.REG_USER_IDNTF_NO
        WHERE p.DEL_YN = 'N'
        ORDER BY p.REG_DT DESC
        """)
    List<PortfolioItem> selectPortfolioList();

    @Select("""
        SELECT
            p.PORTFOLIO_IDNTF_NO,
            p.TITLE,
            p.CONTENT,
            p.THUMBNAIL_ATCH_FILE_IDNTF_NO,
            p.THUMBNAIL_FILE_SN,
            p.REG_DT AS REGISTERED_AT,
            p.INQ_CNT AS VIEW_COUNT,
            p.REG_USER_IDNTF_NO AS REGISTERED_BY_USER_IDNTF_NO,
            u.USER_NM AS REGISTERED_BY_NAME,
            p.DEL_YN,
            p.UPD_DT AS UPDATED_AT
        FROM TN_PORTFOLIO p
        JOIN TN_USER u ON u.USER_IDNTF_NO = p.REG_USER_IDNTF_NO
        WHERE p.PORTFOLIO_IDNTF_NO = #{portfolioIdntfNo}
          AND p.DEL_YN = 'N'
        """)
    PortfolioItem selectPortfolioDetail(@Param("portfolioIdntfNo") String portfolioIdntfNo);

    @Insert("""
        INSERT INTO TN_PORTFOLIO (
            PORTFOLIO_IDNTF_NO,
            TITLE,
            THUMBNAIL_ATCH_FILE_IDNTF_NO,
            THUMBNAIL_FILE_SN,
            CONTENT,
            REG_DT,
            INQ_CNT,
            REG_USER_IDNTF_NO,
            DEL_YN,
            UPD_DT
        ) VALUES (
            #{portfolioIdntfNo},
            #{title},
            #{thumbnailAtchFileIdntfNo},
            #{thumbnailFileSn},
            #{content},
            #{registeredAt},
            0,
            #{registeredByUserIdntfNo},
            'N',
            NULL
        )
        """)
    int insertPortfolio(PortfolioItem portfolioItem);

    @Update("""
        UPDATE TN_PORTFOLIO
        SET
            TITLE = #{title},
            THUMBNAIL_ATCH_FILE_IDNTF_NO = #{thumbnailAtchFileIdntfNo},
            THUMBNAIL_FILE_SN = #{thumbnailFileSn},
            CONTENT = #{content},
            UPD_DT = #{updatedAt}
        WHERE PORTFOLIO_IDNTF_NO = #{portfolioIdntfNo}
          AND DEL_YN = 'N'
        """)
    int updatePortfolio(PortfolioItem portfolioItem);

    @Update("""
        UPDATE TN_PORTFOLIO
        SET
            DEL_YN = 'Y',
            UPD_DT = CURRENT_TIMESTAMP
        WHERE PORTFOLIO_IDNTF_NO = #{portfolioIdntfNo}
        """)
    int deletePortfolio(@Param("portfolioIdntfNo") String portfolioIdntfNo);

    @Update("""
        UPDATE TN_PORTFOLIO
        SET INQ_CNT = INQ_CNT + 1
        WHERE PORTFOLIO_IDNTF_NO = #{portfolioIdntfNo}
          AND DEL_YN = 'N'
        """)
    int incrementViewCount(@Param("portfolioIdntfNo") String portfolioIdntfNo);

    @Select("""
        SELECT COUNT(*)
        FROM TN_PORTFOLIO
        WHERE DEL_YN = 'N'
        """)
    int countPortfolio();
}
