package com.zioinfo.kintex.work.opinion;

import org.apache.ibatis.annotations.*;

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

/** 의견 매퍼(opinion + opinion_comment). */
@Mapper
public interface OpinionMapper {

    @Select("""
            <script>
            SELECT id, event_id AS "eventId", category, title, content, status,
                   author_id AS "authorId", author_name AS "authorName", secret_yn AS "secretYn",
                   to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "createdAt"
            FROM opinion
            <where>
              <if test="eventId != null and eventId != ''">AND event_id = #{eventId}</if>
              <if test="status != null and status != ''">AND status = #{status}</if>
            </where>
            ORDER BY created_at DESC LIMIT #{size} OFFSET #{offset}
            </script>
            """)
    List<Map<String, Object>> search(Map<String, Object> q);

    @Select("""
            <script>
            SELECT count(*) FROM opinion
            <where>
              <if test="eventId != null and eventId != ''">AND event_id = #{eventId}</if>
              <if test="status != null and status != ''">AND status = #{status}</if>
            </where>
            </script>
            """)
    long count(Map<String, Object> q);

    @Select("""
            SELECT id, event_id AS "eventId", category, title, content, status,
                   author_id AS "authorId", author_name AS "authorName", secret_yn AS "secretYn",
                   to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "createdAt"
            FROM opinion WHERE id = #{id}
            """)
    Map<String, Object> findById(@Param("id") String id);

    @Insert("""
            INSERT INTO opinion (id, event_id, category, title, content, status, author_id, author_name, secret_yn)
            VALUES (#{id}, #{eventId}, #{category}, #{title}, #{content}, 'OPEN',
                    #{authorId}, #{authorName}, COALESCE(#{secretYn},'N'))
            """)
    int insert(Map<String, Object> p);

    @Update("UPDATE opinion SET status = #{status}, updated_at = now() WHERE id = #{id}")
    int updateStatus(@Param("id") String id, @Param("status") String status);

    @Delete("DELETE FROM opinion WHERE id = #{id} AND author_id = #{authorId}")
    int delete(@Param("id") String id, @Param("authorId") String authorId);

    @Select("""
            SELECT id, opinion_id AS "opinionId", author_id AS "authorId", author_name AS "authorName", content,
                   to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "createdAt"
            FROM opinion_comment WHERE opinion_id = #{opinionId} ORDER BY created_at
            """)
    List<Map<String, Object>> findComments(@Param("opinionId") String opinionId);

    @Insert("""
            INSERT INTO opinion_comment (id, opinion_id, author_id, author_name, content)
            VALUES (#{id}, #{opinionId}, #{authorId}, #{authorName}, #{content})
            """)
    int insertComment(Map<String, Object> p);
}
