import { desc } from "drizzle-orm";
import { db } from "../db/db.controller.js";
import { logs } from "../db/schema/logs.schema.js";
import type { RoomId } from "./types.socket.js";

type RoomDefinition<T = unknown> = {
	seed: (limit: number) => Promise<T[]>;
};

export const protectedRooms: any = {
	logs: { requiresAuth: true, role: ["admin", "systemAdmin"] },
	//admin: { requiresAuth: false, role: ["admin", "systemAdmin"] },
};

export const roomDefinition: Record<RoomId, RoomDefinition> = {
	logs: {
		seed: async (limit) => {
			try {
				const rows = await db
					.select()
					.from(logs)
					.orderBy(desc(logs.createdAt))
					.limit(limit);

				return rows; //.reverse();
			} catch (e) {
				console.error("Failed to seed logs:", e);
				return [];
			}
		},
	},

	labels: {
		seed: async (limit) => {
			console.info(limit);
			return [];
		},
	},
	admin: {
		seed: async (limit) => {
			console.info(limit);
			return [];
		},
	},
	"admin:build": {
		seed: async (limit) => {
			console.info(limit);
			return [];
		},
	},
};
