package com.zioinfo.mall.uiws.worklog.mapper;

import com.zioinfo.mall.uiws.worklog.model.UiwsWorklog;
import com.zioinfo.mall.uiws.worklog.model.UiwsWorklogCmt;
import com.zioinfo.mall.uiws.worklog.model.UiwsWorklogDtl;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 업무일지 MyBatis 매퍼. 원본 JPA Worklog/WorklogDtl/WorklogCmt Repository 변환 이식.
 * 진행상태 집계는 ported tb_uiws_worklog 기반(코어 TB_CODE 미이식 → 코드값 직접 집계).
 */
@Mapper
public interface WorklogMapper {

    // ── worklog 헤더
    int insertWorklog(UiwsWorklog w);

    int updateWorklog(UiwsWorklog w);

    UiwsWorklog findWorklogById(@Param("worklogId") Long worklogId);

    boolean existsWorklogById(@Param("worklogId") Long worklogId);

    int deleteWorklogById(@Param("worklogId") Long worklogId);

    List<UiwsWorklog> searchList(@Param("from") LocalDate from,
                                 @Param("to") LocalDate to,
                                 @Param("writerId") String writerId,
                                 @Param("progressCd") String progressCd,
                                 @Param("scopeAll") boolean scopeAll,
                                 @Param("ownerIds") List<String> ownerIds,
                                 @Param("limit") int limit,
                                 @Param("offset") int offset);

    long countList(@Param("from") LocalDate from,
                   @Param("to") LocalDate to,
                   @Param("writerId") String writerId,
                   @Param("progressCd") String progressCd,
                   @Param("scopeAll") boolean scopeAll,
                   @Param("ownerIds") List<String> ownerIds);

    List<UiwsWorklog> findByMonth(@Param("from") LocalDate from,
                                  @Param("to") LocalDate to,
                                  @Param("writerId") String writerId,
                                  @Param("scopeAll") boolean scopeAll,
                                  @Param("ownerIds") List<String> ownerIds);

    List<UiwsWorklog> searchPopup(@Param("keyword") String keyword,
                                  @Param("writerId") String writerId,
                                  @Param("scopeAll") boolean scopeAll,
                                  @Param("ownerIds") List<String> ownerIds);

    /** 진행상태별 건수(progress_cd, cnt). */
    List<Map<String, Object>> progressSummary(@Param("from") LocalDate from,
                                              @Param("to") LocalDate to,
                                              @Param("scopeAll") boolean scopeAll,
                                              @Param("ownerIds") List<String> ownerIds);

    // ── 상세(dtl)
    int insertDtl(UiwsWorklogDtl d);

    int updateDtl(UiwsWorklogDtl d);

    int deleteDtlById(@Param("dtlId") Long dtlId);

    List<UiwsWorklogDtl> findDtlByWorklogId(@Param("worklogId") Long worklogId);

    // ── 댓글(cmt)
    int insertCmt(UiwsWorklogCmt c);

    UiwsWorklogCmt findCmtById(@Param("cmtId") Long cmtId);

    int updateCmtKakaoSent(@Param("cmtId") Long cmtId);

    int confirmCmt(@Param("cmtId") Long cmtId, @Param("actor") String actor);

    List<UiwsWorklogCmt> findCmtByWorklogId(@Param("worklogId") Long worklogId);

    List<Map<String, Object>> countCommentsByWorklogIds(@Param("ids") List<Long> ids);

    List<UiwsWorklogCmt> findUnconfirmedByWriter(@Param("writerId") String writerId);

    // ── 사용자명 라벨(코어 user 재사용)
    List<Map<String, String>> findUserNames(@Param("ids") List<String> ids);

    /** 댓글 알림 메일 발송 대상 이메일(username 기준). */
    String findUserEmail(@Param("username") String username);

    List<UiwsWorklog> findWorklogsByIds(@Param("ids") List<Long> ids);
}
