package com.alttd.altitudeweb.database.litebans;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.jetbrains.annotations.NotNull;

public interface EditHistoryMapper {

    @Update("""
            UPDATE ${table_name}
            SET reason = #{reason}
            WHERE id = #{id}
            """)
    int updateReason(@Param("table_name") String tableName,
                     @Param("id") int id,
                     @Param("reason") String reason);

    @Update("""
            UPDATE ${table_name}
            SET until = #{until}
            WHERE id = #{id}
            """)
    int updateUntil(@Param("table_name") String tableName,
                    @Param("id") int id,
                    @Param("until") Long until);

    @Delete("""
            DELETE FROM ${table_name}
            WHERE id = #{id}
            """)
    int deletePunishment(@Param("table_name") String tableName,
                         @Param("id") int id);

    default int setReason(@NotNull HistoryType type, int id, String reason) {
        return switch (type) {
            case ALL -> throw new IllegalArgumentException("HistoryType.ALL is not supported");
            case BAN -> updateReason("litebans_bans", id, reason);
            case MUTE -> updateReason("litebans_mutes", id, reason);
            case KICK -> updateReason("litebans_kicks", id, reason);
            case WARN -> updateReason("litebans_warnings", id, reason);
        };
    }

    default int setUntil(@NotNull HistoryType type, int id, Long until) {
        return switch (type) {
            case ALL -> throw new IllegalArgumentException("HistoryType.ALL is not supported");
            case BAN -> updateUntil("litebans_bans", id, until);
            case MUTE -> updateUntil("litebans_mutes", id, until);
            case KICK -> throw new IllegalArgumentException("KICK has no until");
            case WARN -> updateUntil("litebans_warnings", id, until);
        };
    }

    default int remove(@NotNull HistoryType type, int id) {
        return switch (type) {
            case ALL -> throw new IllegalArgumentException("HistoryType.ALL is not supported");
            case BAN -> deletePunishment("litebans_bans", id);
            case MUTE -> deletePunishment("litebans_mutes", id);
            case KICK -> deletePunishment("litebans_kicks", id);
            case WARN -> deletePunishment("litebans_warnings", id);
        };
    }
}
