import { eq, sql } from "drizzle-orm";
import useDatabase from "~/composables/useDatabase";
import { usersTable } from "~/db/schema";
import type { User } from "~/types/auth";

export default defineEventHandler((e) => {
    const id = getRouterParam(e, 'id');

    if(!id)
    {
        setResponseStatus(e, 400);
        return;
    }

    const db = useDatabase();
    
    const user = db.query.usersTable.findFirst({
        columns: {
            username: true,
        },
        with: {
            data: true
        },
        where: eq(usersTable.id, sql.placeholder('id')),
    }).prepare().get({ id });

    return {
        username: user?.username,
        ...user?.data
    } as User
});