package com.zioinfo.kintex.work.worklog;

import org.apache.ibatis.annotations.*;

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

/** 업무일지 매퍼. DataScope: mineOnly 이면 writer_id 한정. */
@Mapper
public interface WorklogMapper {

    @Select("""
            <script>
            SELECT id, event_id AS "eventId", writer_id AS "writerId", writer_name AS "writerName",
                   to_char(work_date,'YYYY-MM-DD') AS "workDate", title, work_type AS "workType",
                   progress, status, content, hours,
                   to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "createdAt",
                   to_char(updated_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "updatedAt"
            FROM worklog
            <where>
              <if test="mineOnly">AND writer_id = #{userId}</if>
              <if test="eventId != null and eventId != ''">AND event_id = #{eventId}</if>
              <if test="fromDate != null and fromDate != ''">AND work_date &gt;= CAST(#{fromDate} AS date)</if>
              <if test="toDate != null and toDate != ''">AND work_date &lt;= CAST(#{toDate} AS date)</if>
              <if test="status != null and status != ''">AND status = #{status}</if>
            </where>
            ORDER BY work_date DESC, created_at DESC
            LIMIT #{size} OFFSET #{offset}
            </script>
            """)
    List<Map<String, Object>> search(Map<String, Object> q);

    @Select("""
            <script>
            SELECT count(*) FROM worklog
            <where>
              <if test="mineOnly">AND writer_id = #{userId}</if>
              <if test="eventId != null and eventId != ''">AND event_id = #{eventId}</if>
              <if test="fromDate != null and fromDate != ''">AND work_date &gt;= CAST(#{fromDate} AS date)</if>
              <if test="toDate != null and toDate != ''">AND work_date &lt;= CAST(#{toDate} AS date)</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", writer_id AS "writerId", writer_name AS "writerName",
                   to_char(work_date,'YYYY-MM-DD') AS "workDate", title, work_type AS "workType",
                   progress, status, content, hours,
                   to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "createdAt",
                   to_char(updated_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "updatedAt"
            FROM worklog WHERE id = #{id}
            """)
    Map<String, Object> findById(@Param("id") String id);

    @Insert("""
            INSERT INTO worklog (id, event_id, writer_id, writer_name, work_date, title, work_type,
                                 progress, status, content, hours)
            VALUES (#{id}, #{eventId}, #{writerId}, #{writerName}, CAST(#{workDate} AS date), #{title},
                    COALESCE(#{workType},'OPERATION'), COALESCE(#{progress},'P0'),
                    COALESCE(#{status},'TODO'), #{content}, COALESCE(#{hours},0))
            """)
    int insert(Map<String, Object> p);

    @Update("""
            UPDATE worklog SET work_date=CAST(#{workDate} AS date), title=#{title}, work_type=#{workType},
                   progress=#{progress}, status=#{status}, content=#{content}, hours=#{hours},
                   event_id=#{eventId}, updated_at=now()
            WHERE id = #{id} AND writer_id = #{writerId}
            """)
    int update(Map<String, Object> p);

    @Delete("DELETE FROM worklog WHERE id = #{id} AND writer_id = #{writerId}")
    int delete(@Param("id") String id, @Param("writerId") String writerId);
}
