package com.zioinfo.kintex.work.report;

import org.apache.ibatis.annotations.*;

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

/** 보고서 매퍼. */
@Mapper
public interface ReportMapper {

    @Select("""
            <script>
            SELECT id, event_id AS "eventId", report_type AS "reportType",
                   to_char(period_from,'YYYY-MM-DD') AS "periodFrom", to_char(period_to,'YYYY-MM-DD') AS "periodTo",
                   title, content, author_id AS "authorId", author_name AS "authorName",
                   to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "createdAt"
            FROM report
            <where>
              <if test="eventId != null and eventId != ''">AND event_id = #{eventId}</if>
              <if test="reportType != null and reportType != ''">AND report_type = #{reportType}</if>
              <if test="mineOnly">AND author_id = #{userId}</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 report
            <where>
              <if test="eventId != null and eventId != ''">AND event_id = #{eventId}</if>
              <if test="reportType != null and reportType != ''">AND report_type = #{reportType}</if>
              <if test="mineOnly">AND author_id = #{userId}</if>
            </where>
            </script>
            """)
    long count(Map<String, Object> q);

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

    @Insert("""
            INSERT INTO report (id, event_id, report_type, period_from, period_to, title, content,
                                author_id, author_name)
            VALUES (#{id}, #{eventId}, COALESCE(#{reportType},'DAILY'), CAST(#{periodFrom} AS date),
                    CAST(#{periodTo} AS date), #{title}, #{content}, #{authorId}, #{authorName})
            """)
    int insert(Map<String, Object> p);

    @Update("""
            UPDATE report SET report_type=#{reportType}, period_from=CAST(#{periodFrom} AS date),
                   period_to=CAST(#{periodTo} AS date), title=#{title}, content=#{content},
                   event_id=#{eventId}, updated_at=now()
            WHERE id=#{id} AND author_id=#{authorId}
            """)
    int update(Map<String, Object> p);

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