package kr.itn.itnhub.org;

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

import java.util.List;

@Mapper
public interface OrganizationMapper {

    List<Organization> findAll();

    Organization findById(@Param("id") Long id);

    /** 연번+기관명 유니크 키로 단건 조회. 시드가 upsert 직후 방금 반영된 행을 다시 읽을 때 쓴다. */
    Organization findByOrgNoAndOrgName(@Param("orgNo") String orgNo, @Param("orgName") String orgName);

    /** 기관명 단독(정확 일치) 조회. 회원명단 업로드가 신청기관 담당자를 자동 배정할 때 쓴다. */
    Organization findByOrgName(@Param("orgName") String orgName);

    /** 시드 전용. 비어 있는 칸만 채우고 채널ID·기입된 값은 그대로 둔다. */
    int upsertBySeed(Organization org);

    int updateAssignments(@Param("id") Long id,
                          @Param("applicantContactId") Long applicantContactId,
                          @Param("mjContactId") Long mjContactId,
                          @Param("itnContactId") Long itnContactId,
                          @Param("lawyerContactId") Long lawyerContactId,
                          @Param("lawyerAssignedDate") String lawyerAssignedDate);

    int updateChannelIdMj(@Param("id") Long id,
                          @Param("channelId") String channelId);

    int updateChannelIdLaw(@Param("id") Long id,
                           @Param("channelId") String channelId);

    int updateStage(@Param("id") Long id,
                    @Param("stage") Integer stage);

    /**
     * 채널 초기화. 채널 ID 2개와 진행단계를 한 번에 비운다.
     *
     * <p>단계까지 같이 비우는 이유는 "채널 있음 ⇔ 단계 있음" 불변식 때문이다(V11 참고).
     * 채널만 지우고 단계를 남기면 대시보드 칸반에서 채널 없는 기관이 ①신청 컬럼에 남아
     * 드래그도 안 되는 이상한 상태가 된다.</p>
     */
    int clearChannels(@Param("id") Long id);
}
