/*
 *   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.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.time.Instant;

@AllArgsConstructor
public class NodeDownload {

    private static final char[] HEX = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    private static final String LINK_BASE = "https://api.steamwar.de/download/";

    private static final Table<NodeDownload> table = new Table<>(NodeDownload.class);
    private static final Statement insert = table.insertFields("NodeId", "Link");

    private static final SelectStatement<NodeDownload> select = table.selectFields("link");

    private static final Statement delete = table.delete(Table.PRIMARY);

    public static NodeDownload get(String link) {
        return select.select(link);
    }

    @Getter
    @Field(keys = {Table.PRIMARY})
    private final int nodeId;
    @Field
    private final String link;
    @Field(def = "CURRENT_TIMESTAMP")
    @Getter
    private final Timestamp timestamp;

    public static String getLink(SchematicNode schem){
        if(schem.isDir())
            throw new SecurityException("Can not Download Directorys");
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            throw new SecurityException(e);
        }
        digest.reset();
        digest.update((Instant.now().toString() + schem.getOwner() + schem.getId()).getBytes());
        String hash = base16encode(digest.digest());
        insert.update(schem.getId(), hash);
        return LINK_BASE + hash;
    }

    public static String base16encode(byte[] byteArray) {
        StringBuilder hexBuffer = new StringBuilder(byteArray.length * 2);
        for (byte b : byteArray)
            hexBuffer.append(HEX[(b >>> 4) & 0xF]).append(HEX[b & 0xF]);
        return hexBuffer.toString();
    }

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