/*
 *   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.Field;
import de.steamwar.sql.internal.SelectStatement;
import de.steamwar.sql.internal.Statement;
import de.steamwar.sql.internal.Table;
import lombok.AllArgsConstructor;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

@AllArgsConstructor
public class NodeMember {

    public static void init() {
        // enforce class initialization
    }

    private static final Table<NodeMember> table = new Table<>(NodeMember.class);
    private static final SelectStatement<NodeMember> getNodeMember = table.select(Table.PRIMARY);
    private static final SelectStatement<NodeMember> getNodeMembers = table.selectFields("NodeId");
    private static final SelectStatement<NodeMember> getSchematics = table.selectFields("UserId");
    private static final Statement create = table.insert(Table.PRIMARY);
    private static final Statement delete = table.delete(Table.PRIMARY);
    private static final Statement updateParent = table.update(Table.PRIMARY, "ParentId");

    @Field(keys = {Table.PRIMARY})
    private final int nodeId;
    @Field(keys = {Table.PRIMARY})
    private final int userId;
    @Field(nullable = true, def = "null")
    private Integer parentId;

    public int getNode() {
        return nodeId;
    }

    public int getMember() {
        return userId;
    }

    public Optional<Integer> getParent() {
        return Optional.ofNullable(parentId);
    }

    public void delete() {
        delete.update(nodeId, userId);
    }

    public static NodeMember createNodeMember(int node, int member) {
        create.update(node, member);
        return new NodeMember(node, member, null);
    }

    public static NodeMember getNodeMember(int node, SteamwarUser member) {
        return getNodeMember(node, member.getId());
    }

    public static NodeMember getNodeMember(int node, int member) {
        return getNodeMember.select(node, member);
    }

    public static Set<NodeMember> getNodeMembers(int node) {
        return new HashSet<>(getNodeMembers.listSelect(node));
    }

    public static Set<NodeMember> getSchematics(int member) {
        return new HashSet<>(getSchematics.listSelect(member));
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
        updateParent.update(this.parentId, nodeId, userId);
    }
}
