package com.pandoli365.bibimbap.mapper;

import com.pandoli365.bibimbap.data.TagData;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface JamTagsMapper {

    @Insert("""
            <script>
            INSERT INTO jam_tags (jam_id, tag_id) VALUES
            <foreach item="tid" collection="tagIds" separator=",">(#{jamId}, #{tid})</foreach>
            ON CONFLICT (jam_id, tag_id) DO NOTHING
            </script>
            """)
    void attachTags(@Param("jamId") long jamId, @Param("tagIds") List<Long> tagIds);

    @Select("""
            SELECT
                t.id,
                t.name,
                t.slug,
                t.tag_type AS tagType,
                t.is_active AS isActive,
                t.created_by AS createdBy,
                t.created_at AS createdAt
            FROM jam_tags jt
            JOIN tags t ON t.id = jt.tag_id
            WHERE jt.jam_id = #{jamId}
              AND t.is_active = TRUE
            ORDER BY t.name ASC
            """)
    List<TagData> listByJam(long jamId);
}
