/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.sql;

import de.steamwar.sql.internal.*;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.*;
import java.util.stream.Collectors;

public enum UserPerm {
    PREFIX_NONE, // special value, not stored in database
    PREFIX_YOUTUBER,
    PREFIX_GUIDE,
    PREFIX_BUILDER,
    PREFIX_SUPPORTER,
    PREFIX_MODERATOR,
    PREFIX_DEVELOPER,
    PREFIX_ADMIN,

    RESTRICTED_MODS,
    COLOR_CHAT,
    TEAM,
    TICKET_LOG,
    BUILD,
    CHECK,
    MODERATION,
    ADMINISTRATION;

    public static final Map<UserPerm, Prefix> prefixes;
    public static final Prefix emptyPrefix;
    static {
        // https://www.digminecraft.com/lists/color_list_pc.php
        SqlTypeMapper.nameEnumMapper(UserPerm.class);
        Map<UserPerm, Prefix> p = new EnumMap<>(UserPerm.class);
        emptyPrefix = new Prefix("§7", "");
        p.put(PREFIX_NONE, emptyPrefix);
        p.put(PREFIX_YOUTUBER, new Prefix("§7", "YT"));
        p.put(PREFIX_GUIDE, new Prefix("§x§e§7§6§2§e§d", "Guide")); // E762ED

        p.put(PREFIX_SUPPORTER, new Prefix("§x§6§0§9§5§F§B", "Sup")); // 6095FB
        p.put(PREFIX_MODERATOR, new Prefix("§x§F§F§A§2§5§C", "Mod")); // FFA25C
        p.put(PREFIX_BUILDER, new Prefix("§x§6§0§F§F§6§A", "Arch")); // 60FF6A
        p.put(PREFIX_DEVELOPER, new Prefix("§x§0§B§B§C§B§3", "Dev")); // 0BBCB3
        p.put(PREFIX_ADMIN, new Prefix("§x§F§F§2§B§2§4", "Admin")); // FF2B24
        prefixes = Collections.unmodifiableMap(p);
    }

    private static final Table<UserPermTable> table = new Table<>(UserPermTable.class, "UserPerm");
    private static final SelectStatement<UserPermTable> getPerms = table.selectFields("user");
    private static final Statement addPerm = table.insertAll();
    private static final Statement removePerm = table.delete(Table.PRIMARY);

    public static Set<UserPerm> getPerms(int user) {
        return getPerms.listSelect(user).stream().map(up -> up.perm).collect(Collectors.toSet());
    }

    public static void addPerm(SteamwarUser user, UserPerm perm) {
        addPerm.update(user, perm);
    }

    public static void removePerm(SteamwarUser user, UserPerm perm) {
        removePerm.update(user, perm);
    }

    @Getter
    @AllArgsConstructor
    public static class Prefix {
        private final String colorCode;
        private final String chatPrefix;
    }

    @AllArgsConstructor
    public static class UserPermTable {
        @Field(keys = {Table.PRIMARY})
        private final int user;
        @Field(keys = {Table.PRIMARY})
        private final UserPerm perm;
    }
}
