import { db } from '$lib/server/db';
import { character, devilFruit, arc, user } from '$lib/server/db/schema';
import { getOrCreateTodayCharacter, getTodayCharacterWinsCount } from '$lib/server/daily-character';
import type { PageServerLoad } from './$types';

export const load: PageServerLoad = async () => {
	const [characters, devilFruits, arcs, users] = await Promise.all([
		db.select().from(character),
		db.select().from(devilFruit),
		db.select().from(arc),
		db.select().from(user)
	]);

	// Get today's daily character and count wins
	const todayCharacter = await getOrCreateTodayCharacter();
	
	let dailyCharacterWins = 0;
	if (todayCharacter) {
		dailyCharacterWins = await getTodayCharacterWinsCount(todayCharacter.id);
	}

	return {
		stats: {
			totalCharacters: characters.length,
			charactersInDaily: characters.filter((c) => c.isInDailyMode).length,
			totalDevilFruits: devilFruits.length,
			totalArcs: arcs.length,
			totalUsers: users.length,
			adminUsers: users.filter((u) => u.isAdmin).length,
			dailyCharacterWins
		}
	};
};
