import {userSnowflakeToUuid, UsersTable} from "../users.js"
import {Snowflake} from "discord-api-types/globals.js"

interface InMemoryUserData {
    id: string,
    is_admin: boolean
    active_at: Date | null
}

export class InMemoryUsersTable implements UsersTable {
    private readonly _users: Record<string, InMemoryUserData> = {}

    async createBotOwnerAsAdmin(snowflake: Snowflake): Promise<void> {
        const uuid = userSnowflakeToUuid(snowflake)
        this._users[uuid] = {
            id: uuid,
            is_admin: true,
            active_at: null,
        }
    }

    async getActiveSnowflakeIsAdmin(snowflake: Snowflake): Promise<boolean> {
        const uuid = userSnowflakeToUuid(snowflake)
        if (!this._users.hasOwnProperty(uuid)) {
            this._users[uuid] = {
                id: uuid,
                is_admin: false,
                active_at: new Date(),
            }
        }
        const user = this._users[uuid]
        return user.is_admin
    }

    async getUuidForActiveSnowflake(snowflake: Snowflake): Promise<string> {
        const uuid = userSnowflakeToUuid(snowflake)
        if (!this._users.hasOwnProperty(uuid)) {
            this._users[uuid] = {
                id: uuid,
                is_admin: false,
                active_at: new Date(),
            }
        }
        return uuid
    }
}