import dotenv from 'dotenv';
import chalk from 'chalk';
import jwt from 'jsonwebtoken'
import { randomUUID } from 'node:crypto'
dotenv.config();

export default class AuthHandler {
    ips = new Map()
    #secret

    constructor() {
        this.#secret = process.env.JWT_SECRET || 'random-id-for-now-123013123u1o23o12i3ukjdsbvkfndijnx1ijs';
    }

    check(req, res) {
        if(this.ips.get(req.headers["x-forwarded-for"])) {
            console.log(chalk.green("        ", req.headers["x-forwarded-for"]))
            return true
        } else {
            console.log(chalk.red("        ", req.headers["x-forwarded-for"]))
            return false
        }
    }

    login(req, res) {
        const { login } = req.body;
        if(login === process.env.LOGIN) {
            this.ips.set(req.headers["x-forwarded-for"], new Date())
            return true;
        } else {
            return false
        }
    }

    sign(payload, options = {}) {
        return jwt.sign(
            payload,
            this.#secret,
            { expiresIn: '30d', ...options }
        );
    }

    verify(token) {
        return jwt.verify(token, this.#secret)
    }
}