package com.zioinfo.kintex.work.schedule;

import org.apache.ibatis.annotations.*;

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

/** 일정 매퍼. 조회는 기간 범위 + DataScope. */
@Mapper
public interface ScheduleMapper {

    @Select("""
            <script>
            SELECT id, event_id AS "eventId", owner_id AS "ownerId", owner_name AS "ownerName", title,
                   schedule_type AS "scheduleType", importance,
                   to_char(start_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "startAt",
                   to_char(end_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "endAt",
                   all_day AS "allDay", location, content, color,
                   to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "createdAt"
            FROM schedule
            <where>
              <if test="mineOnly">AND owner_id = #{userId}</if>
              <if test="eventId != null and eventId != ''">AND event_id = #{eventId}</if>
              <if test="fromAt != null and fromAt != ''">AND start_at &gt;= CAST(#{fromAt} AS timestamptz)</if>
              <if test="toAt != null and toAt != ''">AND start_at &lt;= CAST(#{toAt} AS timestamptz)</if>
            </where>
            ORDER BY start_at
            </script>
            """)
    List<Map<String, Object>> search(Map<String, Object> q);

    @Select("""
            SELECT id, event_id AS "eventId", owner_id AS "ownerId", owner_name AS "ownerName", title,
                   schedule_type AS "scheduleType", importance,
                   to_char(start_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "startAt",
                   to_char(end_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "endAt",
                   all_day AS "allDay", location, content, color,
                   to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "createdAt"
            FROM schedule WHERE id = #{id}
            """)
    Map<String, Object> findById(@Param("id") String id);

    @Insert("""
            INSERT INTO schedule (id, event_id, owner_id, owner_name, title, schedule_type, importance,
                                  start_at, end_at, all_day, location, content, color)
            VALUES (#{id}, #{eventId}, #{ownerId}, #{ownerName}, #{title},
                    COALESCE(#{scheduleType},'PERSONAL'), COALESCE(#{importance},'NORMAL'),
                    CAST(#{startAt} AS timestamptz), CAST(#{endAt} AS timestamptz),
                    COALESCE(#{allDay},false), #{location}, #{content}, #{color})
            """)
    int insert(Map<String, Object> p);

    @Update("""
            UPDATE schedule SET title=#{title}, schedule_type=#{scheduleType}, importance=#{importance},
                   start_at=CAST(#{startAt} AS timestamptz), end_at=CAST(#{endAt} AS timestamptz),
                   all_day=COALESCE(#{allDay},false), location=#{location}, content=#{content},
                   color=#{color}, event_id=#{eventId}, updated_at=now()
            WHERE id=#{id} AND owner_id=#{ownerId}
            """)
    int update(Map<String, Object> p);

    @Delete("DELETE FROM schedule WHERE id=#{id} AND owner_id=#{ownerId}")
    int delete(@Param("id") String id, @Param("ownerId") String ownerId);
}
