/*
 *   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 lombok.Getter;

import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@AllArgsConstructor
public class CheckedSchematic {

    private static final Table<CheckedSchematic> table = new Table<>(CheckedSchematic.class);
    private static final SelectStatement<CheckedSchematic> statusOfNode = new SelectStatement<>(table, "SELECT * FROM CheckedSchematic WHERE NodeId = ? AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC");
    private static final SelectStatement<CheckedSchematic> nodeHistory = new SelectStatement<>(table, "SELECT * FROM CheckedSchematic WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC");
    private static final Statement insert = table.insertAll();

    private static final SelectStatement<CheckedSchematic> getUnseen = new SelectStatement<>(table, "SELECT * FROM CheckedSchematic WHERE Seen = 0 AND NodeOwner = ? ORDER BY StartTime DESC");
    private static final Statement updateSeen = new Statement("UPDATE CheckedSchematic SET Seen = ? WHERE StartTime = ? AND EndTime = ? AND NodeName = ?");

    public static void create(SchematicNode node, int validator, Timestamp startTime, Timestamp endTime, String reason, boolean seen) {
        insert.update(node.getId(), node.getOwner(), node.getName(), validator, startTime, endTime, reason, seen, node.getSchemtype().toDB().substring(1));
    }

    public static List<CheckedSchematic> getLastDeclinedOfNode(int node) {
        return statusOfNode.listSelect(node);
    }

    public static List<CheckedSchematic> previousChecks(SchematicNode node) {
        return nodeHistory.listSelect(node.getId());
    }

    public static List<CheckedSchematic> getUnseen(SteamwarUser owner) {
        return getUnseen.listSelect(owner);
    }

    @Field(nullable = true)
    private final Integer nodeId;
    @Field
    private final int nodeOwner;
    @Field
    private final String nodeName;
    @Getter
    @Field
    private final int validator;
    @Getter
    @Field
    private final Timestamp startTime;
    @Getter
    @Field
    private final Timestamp endTime;
    @Getter
    @Field
    private final String declineReason;
    @Getter
    @Field
    private boolean seen;
    @Getter
    @Field
    private final String nodeType;

    public int getNode() {
        return nodeId;
    }

    public String getSchemName() {
        return nodeName;
    }

    public int getSchemOwner() {
        return nodeOwner;
    }

    public void setSeen(boolean seen) {
        this.seen = seen;
        updateSeen.update(seen, startTime, endTime, nodeName);
    }
}
