import { eq } from "drizzle-orm";
import useDatabase from "~/composables/useDatabase";
import { usersTable } from "~/db/schema";

export default defineEventHandler(async (e) => {
    const session = await getUserSession(e);

    if(!session.user)
    {
        setResponseStatus(e, 404);
        return;
    }
    const id = getRouterParam(e, 'id');

    if(!id)
    {
        setResponseStatus(e, 400);
        return;
    }
    if(session.user.id.toString() !== id)
    {
        setResponseStatus(e, 403);
        return;
    }

    const db = useDatabase();
    
    clearUserSession(e);
    db.delete(usersTable).where(eq(usersTable.id, session.user.id)).run();
});