import type { RequestHandler } from './$types';
import { db } from '$lib/server/db';
import { dailyCompletions } from '$lib/server/db/schema';
import { and, eq, asc } from 'drizzle-orm';
import { json } from '@sveltejs/kit';

export const GET: RequestHandler = async ({ url }) => {
	try {
		const anonymousId = url.searchParams.get('anonymousId');
		const date = url.searchParams.get('date');

		if (!anonymousId || !date) {
			return json({ error: 'Invalid data' }, { status: 400 });
		}

		const userCompletions = await db
			.select()
			.from(dailyCompletions)
			.where(and(
				eq(dailyCompletions.anonymousId, anonymousId),
				eq(dailyCompletions.date, date)
			))
			.limit(1);

		if (userCompletions.length === 0) {
			return json({ error: 'No completion found' }, { status: 404 });
		}

		const userCompletion = userCompletions[0];
		const guessCount = userCompletion.guessCount;

		const allCompletions = await db
			.select()
			.from(dailyCompletions)
			.where(eq(dailyCompletions.date, date))
			.orderBy(asc(dailyCompletions.completedAt));

		const totalSolves = allCompletions.length;

		const solveRank = allCompletions.findIndex(c => c.anonymousId === anonymousId) + 1;

		const betterGuesses = allCompletions.filter(c => c.guessCount < guessCount).length;
		const guessRank = betterGuesses + 1;

		const tiedCount = allCompletions.filter(c => c.guessCount === guessCount && c.anonymousId !== anonymousId).length;

		const totalGuesses = allCompletions.reduce((sum, c) => sum + c.guessCount, 0);
		const averageGuesses = Math.round((totalGuesses / totalSolves) * 10) / 10;

		const betterOrEqualCount = allCompletions.filter(c => c.guessCount <= guessCount).length;
		const percentile = Math.round((betterOrEqualCount / totalSolves) * 100);

		const guesses = userCompletion.guesses ? JSON.parse(userCompletion.guesses) : undefined;

		return json({
			success: true,
			stats: { solveRank, guessRank, totalSolves, averageGuesses, tiedCount, percentile, guesses }
		});
	} catch (err) {
		console.error('Error fetching stats:', err);
		return json({ error: 'Failed to fetch stats' }, { status: 500 });
	}
};
