package kr.itn.itnhub.review;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface ReviewItemMapper {

    /**
     * 순번 오름차순. keyword는 사이트명/게시판명/게시물제목을 OR로 훑는 통합검색이고,
     * site/board/title은 각 열을 따로 걸러 서로 AND로 묶이는 복합검색이다(요청자 요구:
     * "관광공사의 A 게시판 중 B가 포함된 게시물"처럼 조건을 겹쳐 찾는 용도). null이면 무시된다.
     */
    List<ReviewItem> findPage(@Param("orgId") Long orgId, @Param("keyword") String keyword,
                               @Param("site") String site, @Param("board") String board,
                               @Param("title") String title,
                               @Param("offset") int offset, @Param("limit") int limit);

    int countByOrg(@Param("orgId") Long orgId, @Param("keyword") String keyword,
                   @Param("site") String site, @Param("board") String board,
                   @Param("title") String title);

    /**
     * 여러 건에 같은 값을 한 번에 반영한다(일괄 등록). null로 준 항목은 건드리지 않는다.
     * 개별 저장과 마찬가지로 web_edited_at을 채워, 엑셀 재업로드 때 이 값이 덮이지 않게 한다.
     */
    int updateJudgmentBulk(@Param("orgId") Long orgId, @Param("ids") List<Long> ids,
                           @Param("reviewResult") String reviewResult,
                           @Param("judgedKoglType") String judgedKoglType,
                           @Param("opinion") String opinion);

    /** 일괄 삭제. 목록에서 여러 건을 골라 지울 때 쓴다. */
    int deleteByIds(@Param("orgId") Long orgId, @Param("ids") List<Long> ids);

    /** 처리결과(review_result)가 채워진 행 수 - 검색어와 무관한 기관 전체 진행률. */
    int countDone(@Param("orgId") Long orgId);

    /** 다운로드/재업로드 판정용으로 페이징 없이 순번순 전체를 읽는다. */
    List<ReviewItem> findAllByOrg(@Param("orgId") Long orgId);

    /** 이미 저장된 순번 전체 - SeedService처럼 신규/갱신 집계를 메모리에서 판단하기 위함. */
    List<Integer> findSeqsByOrg(@Param("orgId") Long orgId);

    ReviewItem findById(@Param("orgId") Long orgId, @Param("id") Long id);

    /**
     * 엑셀 재업로드 1행 반영. (org_id, seq) 충돌 시 조사원 영역은 항상 덮어쓰고,
     * 변호사 영역은 web_edited_at이 null일 때만 덮어쓴다 - 정책 세부는 XML 주석 참고.
     */
    int upsertFromFile(ReviewItem item);

    int updateJudgment(@Param("orgId") Long orgId, @Param("id") Long id,
                       @Param("hasAttachment") String hasAttachment,
                       @Param("koglAttached") String koglAttached,
                       @Param("koglType") String koglType,
                       @Param("aiType") String aiType,
                        @Param("openable") String openable,
                        @Param("reviewMajor") String reviewMajor,
                        @Param("reviewMinor") String reviewMinor,
                        @Param("reviewResult") String reviewResult,
                        @Param("judgedKoglType") String judgedKoglType,
                        @Param("judgedAiType") String judgedAiType,
                        @Param("opinion") String opinion,
                        @Param("lawyerNote") String lawyerNote,
                        @Param("needsProcessing") String needsProcessing);

    int deleteById(@Param("orgId") Long orgId, @Param("id") Long id);

    /** 기관 삭제 캐스케이드 외에는 아직 쓰이지 않지만, 저장 비용이 낮아 함께 둔다. */
    int deleteByOrg(@Param("orgId") Long orgId);
}
