package com.alttd.altitudeweb.database.litebans;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

public interface IdHistoryMapper {
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "uuid", column = "uuid"),
            @Result(property = "punishedName", column = "punished_name"),
            @Result(property = "reason", column = "reason"),
            @Result(property = "bannedByUuid", column = "banned_by_uuid"),
            @Result(property = "bannedByName", column = "banned_by_name"),
            @Result(property = "removedByName", column = "removed_by_name"),
            @Result(property = "time", column = "time"),
            @Result(property = "until", column = "until"),
            @Result(property = "removedByReason", column = "removed_by_reason")
    })
    @Select("""
            SELECT punishment.id,
                   punishment.uuid,
                   user_lookup.name AS punished_name,
                   punishment.reason,
                   punishment.banned_by_uuid,
                   punishment.banned_by_name,
                   punishment.removed_by_name,
                   punishment.time,
                   punishment.until,
                   punishment.removed_by_reason
            FROM (SELECT *
                  FROM ${table_name} AS punishment
                  WHERE punishment.id = #{id})
                     AS punishment
            INNER JOIN user_lookup
                ON user_lookup.uuid = punishment.uuid
            """)
    HistoryRecord getHistoryForId(@Param("table_name") String tableName,
                                  @Param("type") String type,
                                  @Param("id") int id);

    default HistoryRecord getRecentHistory(HistoryType historyType, int id) {
        HistoryRecord historyRecord = switch (historyType) {
            case ALL -> throw new IllegalArgumentException("HistoryType.ALL is not supported");
            case BAN -> getHistoryForId("litebans_bans", "ban", id);
            case MUTE -> getHistoryForId("litebans_mutes", "mute", id);
            case KICK -> getHistoryForId("litebans_kicks", "kick", id);
            case WARN -> getHistoryForId("litebans_warnings", "warn", id);
        };
        historyRecord.setType(historyType.name().toLowerCase());
        return historyRecord;
    }
}
