package com.zioinfo.kintex.work.meeting;

import org.apache.ibatis.annotations.*;

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

/** 회의 매퍼(meeting + meeting_action). */
@Mapper
public interface MeetingMapper {

    @Select("""
            <script>
            SELECT id, event_id AS "eventId", title, location,
                   to_char(meeting_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "meetingAt",
                   organizer_id AS "organizerId", organizer_name AS "organizerName", content, minutes,
                   to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "createdAt"
            FROM meeting
            <where>
              <if test="eventId != null and eventId != ''">AND event_id = #{eventId}</if>
            </where>
            ORDER BY meeting_at DESC LIMIT #{size} OFFSET #{offset}
            </script>
            """)
    List<Map<String, Object>> search(Map<String, Object> q);

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

    @Select("""
            SELECT id, event_id AS "eventId", title, location,
                   to_char(meeting_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "meetingAt",
                   organizer_id AS "organizerId", organizer_name AS "organizerName", content, minutes,
                   to_char(created_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "createdAt"
            FROM meeting WHERE id = #{id}
            """)
    Map<String, Object> findById(@Param("id") String id);

    @Insert("""
            INSERT INTO meeting (id, event_id, title, location, meeting_at, organizer_id, organizer_name, content)
            VALUES (#{id}, #{eventId}, #{title}, #{location}, CAST(#{meetingAt} AS timestamptz),
                    #{organizerId}, #{organizerName}, #{content})
            """)
    int insert(Map<String, Object> p);

    @Update("""
            UPDATE meeting SET title=#{title}, location=#{location},
                   meeting_at=CAST(#{meetingAt} AS timestamptz), content=#{content},
                   event_id=#{eventId}, updated_at=now()
            WHERE id=#{id} AND organizer_id=#{organizerId}
            """)
    int update(Map<String, Object> p);

    @Update("UPDATE meeting SET minutes=#{minutes}, updated_at=now() WHERE id=#{id}")
    int updateMinutes(@Param("id") String id, @Param("minutes") String minutes);

    @Delete("DELETE FROM meeting WHERE id=#{id} AND organizer_id=#{organizerId}")
    int delete(@Param("id") String id, @Param("organizerId") String organizerId);

    @Select("""
            SELECT id, meeting_id AS "meetingId", seq, action_item AS "actionItem", assignee_id AS "assigneeId",
                   to_char(due_date,'YYYY-MM-DD') AS "dueDate", status
            FROM meeting_action WHERE meeting_id=#{meetingId} ORDER BY seq
            """)
    List<Map<String, Object>> findActions(@Param("meetingId") String meetingId);

    @Select("SELECT COALESCE(MAX(seq),0) FROM meeting_action WHERE meeting_id=#{meetingId}")
    int maxSeq(@Param("meetingId") String meetingId);

    @Insert("""
            INSERT INTO meeting_action (id, meeting_id, seq, action_item, assignee_id, due_date, status)
            VALUES (#{id}, #{meetingId}, #{seq}, #{actionItem}, #{assigneeId},
                    CAST(#{dueDate} AS date), 'TODO')
            """)
    int insertAction(Map<String, Object> p);

    @Update("UPDATE meeting_action SET status=#{status} WHERE id=#{id}")
    int updateActionStatus(@Param("id") String id, @Param("status") String status);
}
