package com.zioinfo.kintex.visitor.notification;

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

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

/**
 * F-C5 관람객 인앱 알림함 매퍼. tenant_id='KINTEX' 고정. 본인(user_id) 알림만.
 * <p>★ camelCase 별칭 쌍따옴표({@code AS "x"}).
 */
@Mapper
public interface VisitorNotificationMapper {

    /** 알림 삽입(대기 승격·RSVP·멤버십 등). */
    @Insert("""
            INSERT INTO visitor_notification
                (tenant_id, id, user_id, kind, title, body, ref_type, ref_id)
            VALUES
                ('KINTEX', #{id}, #{userId}, #{kind}, #{title}, #{body}, #{refType}, #{refId})
            """)
    void insert(Map<String, Object> row);

    /** 내 알림 목록(최신순). */
    @Select("""
            SELECT id,
                   kind,
                   title,
                   body,
                   ref_type AS "refType",
                   ref_id   AS "refId",
                   is_read  AS "read",
                   to_char(created_at, 'YYYY-MM-DD"T"HH24:MI:SS') AS "createdAt"
            FROM visitor_notification
            WHERE tenant_id = 'KINTEX' AND user_id = #{userId}
            ORDER BY created_at DESC
            LIMIT 100
            """)
    List<Map<String, Object>> listByUser(@Param("userId") String userId);

    /** 미읽음 수. */
    @Select("SELECT count(*) FROM visitor_notification WHERE tenant_id='KINTEX' AND user_id = #{userId} AND is_read = false")
    int unreadCount(@Param("userId") String userId);

    /** 단건 읽음(본인 소유분만). */
    @Update("UPDATE visitor_notification SET is_read = true WHERE tenant_id='KINTEX' AND id = #{id} AND user_id = #{userId}")
    int markRead(@Param("id") String id, @Param("userId") String userId);

    /** 전체 읽음. */
    @Update("UPDATE visitor_notification SET is_read = true WHERE tenant_id='KINTEX' AND user_id = #{userId} AND is_read = false")
    int markAllRead(@Param("userId") String userId);
}
