/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2023  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 lombok.SneakyThrows;
import lombok.ToString;

import java.security.MessageDigest;
import java.security.SecureRandom;
import java.sql.Timestamp;
import java.util.Base64;
import java.util.List;

@AllArgsConstructor
@Getter
@ToString
public class Token {
    private static final Table<Token> table = new Table<>(Token.class);
    private static final Statement insert = table.insertFields(true, "Name", "Owner", "Hash");
    private static final SelectStatement<Token> get = table.select(Table.PRIMARY);
    private static final SelectStatement<Token> listUser = table.selectFields("owner");
    private static final SelectStatement<Token> getHash = table.selectFields("hash");
    private static final Statement delete = table.delete(Table.PRIMARY);

    @SneakyThrows
    private static String getHash(String code) {
        return Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-512").digest(code.getBytes()));
    }

    @SneakyThrows
    public static String createToken(String name, SteamwarUser owner) {
        SecureRandom random = new SecureRandom();
        byte[] bytes = new byte[20];
        random.nextBytes(bytes);

        String code = Base64.getEncoder().encodeToString(bytes);

        String hash = getHash(code);
        create(name, owner, hash);
        return code;
    }

    public static Token getTokenByCode(String code) {
        String hash = getHash(code);
        return get(hash);
    }

    public static Token create(String name, SteamwarUser owner, String hash) {
        int id = insert.insertGetKey(name, owner, hash);
        return get(id);
    }

    public static Token get(int id) {
        return get.select(id);
    }

    public static List<Token> listUser(SteamwarUser owner) {
        return listUser.listSelect(owner);
    }

    public static Token get(String hash) {
        return getHash.select(hash);
    }

    public static void delete(Token id) {
        delete.update(id.getId());
    }

    @Field(keys = Table.PRIMARY, autoincrement = true)
    private final int id;
    @Field(keys = "NameOwner")
    private final String name;
    @Field(keys = "NameOwner")
    private final int owner;
    @Field
    private final Timestamp created;
    @Field
    private final String hash;

    public void delete() {
        delete(this);
    }

    public SteamwarUser getOwner() {
        return SteamwarUser.get(owner);
    }
}
