package com.zioinfo.kintex.work.search;

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

import java.util.List;
import java.util.Map;

/**
 * 통합검색 매퍼 — 공통 업무 테이블 + 도메인(행사·업체·옥션·부스·리드·CMS)을 UNION 검색.
 * DataScope: seeAll 이 false 면 worklog/report/opinion 은 본인 것 + 비밀 아님만.
 * 도메인: event 는 tenant_id='KINTEX' 스코프, lead 는 PII 원문(name/phone/email) 미노출(company·product 만).
 */
@Mapper
public interface SearchMapper {

    @Select("""
            <script>
            SELECT * FROM (
              SELECT 'WORKLOG' AS type, id, title,
                     to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "createdAt", created_at AS ord
              FROM worklog
              WHERE title ILIKE '%'||#{q}||'%' <if test="seeAll == false">AND writer_id = #{userId}</if>
              UNION ALL
              SELECT 'NOTICE', id, title,
                     to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"'), created_at
              FROM notice WHERE title ILIKE '%'||#{q}||'%'
              UNION ALL
              SELECT 'MEETING', id, title,
                     to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"'), created_at
              FROM meeting WHERE title ILIKE '%'||#{q}||'%'
              UNION ALL
              SELECT 'REPORT', id, title,
                     to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"'), created_at
              FROM report WHERE title ILIKE '%'||#{q}||'%' <if test="seeAll == false">AND author_id = #{userId}</if>
              UNION ALL
              SELECT 'OPINION', id, title,
                     to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"'), created_at
              FROM opinion
              WHERE title ILIKE '%'||#{q}||'%'
                AND (secret_yn = 'N' <if test="seeAll == false">OR author_id = #{userId}</if>)
              UNION ALL
              SELECT 'EVENT', id, name,
                     to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"'), created_at
              FROM event WHERE tenant_id = 'KINTEX' AND name ILIKE '%'||#{q}||'%'
              UNION ALL
              SELECT 'COMPANY', id, name,
                     to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"'), created_at
              FROM company WHERE name ILIKE '%'||#{q}||'%'
              UNION ALL
              SELECT 'AUCTION', id, title,
                     to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"'), created_at
              FROM auction WHERE title ILIKE '%'||#{q}||'%'
              UNION ALL
              SELECT 'BOOTH', id, COALESCE(booth_no, booth_id),
                     to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"'), created_at
              FROM booth_sale WHERE tenant_id = 'KINTEX' AND booth_no ILIKE '%'||#{q}||'%'
              UNION ALL
              SELECT 'LEAD', id,
                     COALESCE(NULLIF(concat_ws(' · ', company, product), ''), '리드'),
                     to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"'), created_at
              FROM lead WHERE company ILIKE '%'||#{q}||'%' OR product ILIKE '%'||#{q}||'%'
              UNION ALL
              SELECT 'CMS', id, title,
                     to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"'), created_at
              FROM cms_content WHERE title ILIKE '%'||#{q}||'%'
            ) s ORDER BY ord DESC LIMIT #{size}
            </script>
            """)
    List<Map<String, Object>> searchAll(@Param("q") String q, @Param("userId") String userId,
                                        @Param("seeAll") boolean seeAll, @Param("size") int size);
}
