import { redis, RedisClient } from "bun";

class LockManager {
	private redis: RedisClient;

	/*
	 * Create a new LockManager
	 * @param redisClient The Redis client used to store the lock data
	 */
	constructor(redisClient: RedisClient) {
		this.redis = redisClient;
	}

	/*
	 * Acquire a lock for a given ID
	 * @param id The unique identifier for the lock
	 * @param timeout Optional timeout in seconds after which the lock will automatically be released
	 * @returns true if the lock was successfully acquired, false otherwise
	 */
	async acquireLock(id: string, timeout?: number): Promise<boolean> {
		const key = `cvsa:lock:${id}`;
		const result = await this.redis.set(key, "locked", "NX");

		if (result !== "OK") {
			return false;
		}
		if (timeout) {
			await this.redis.expire(key, timeout);
		}
		return true;
	}

	/*
	 * Release a lock for a given ID
	 * @param id The unique identifier for the lock
	 * @returns true if the lock was successfully released, false otherwise
	 */
	async releaseLock(id: string): Promise<boolean> {
		const key = `cvsa:lock:${id}`;
		const result = await this.redis.del(key);
		return result === 1;
	}

	/*
	 * Check if a lock is currently held for a given ID
	 * @param id The unique identifier for the lock
	 * @returns true if the lock is currently held, false otherwise
	 */
	async isLocked(id: string): Promise<boolean> {
		const key = `cvsa:lock:${id}`;
		return await this.redis.exists(key);
	}
}

export const lockManager = new LockManager(redis);
