package com.pandoli365.bibimbap.mapper;

import com.pandoli365.bibimbap.data.JamTeamData;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface JamTeamsMapper {

    @Insert("""
            INSERT INTO jam_teams (
                jam_id,
                name,
                owner_user_id
            ) VALUES (
                #{jamId},
                #{name},
                #{ownerUserId}
            )
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insert(JamTeamData team);

    @Select("""
            SELECT
                id,
                jam_id AS jamId,
                name,
                owner_user_id AS ownerUserId,
                created_at AS createdAt,
                is_delete AS isDelete
            FROM jam_teams
            WHERE id = #{teamId}
              AND is_delete IS NOT TRUE
            """)
    JamTeamData getById(long teamId);

    @Select("""
            SELECT
                t.id,
                t.jam_id AS jamId,
                t.name,
                t.owner_user_id AS ownerUserId,
                t.created_at AS createdAt,
                t.is_delete AS isDelete,
                (SELECT COUNT(*) FROM jam_team_members m WHERE m.jam_team_id = t.id) AS memberCount
            FROM jam_teams t
            WHERE t.jam_id = #{jamId}
              AND t.is_delete IS NOT TRUE
            ORDER BY t.created_at ASC, t.id ASC
            """)
    List<JamTeamData> listByJam(long jamId);
}
