import { users } from "@db/schema";
import { eq } from "drizzle-orm";
import { DrizzleClient } from "@shared/db/DrizzleClient";
import { withTransaction } from "@/lib/db";
import type { Transaction } from "@shared/lib/types";

export const userService = {
    getUserById: async (id: string) => {
        const user = await DrizzleClient.query.users.findFirst({
            where: eq(users.id, BigInt(id)),
            with: { class: true }
        });
        return user;
    },
    getUserByUsername: async (username: string) => {
        const user = await DrizzleClient.query.users.findFirst({ where: eq(users.username, username) });
        return user;
    },
    getOrCreateUser: async (id: string, username: string, tx?: Transaction) => {
        return await withTransaction(async (txFn) => {
            let user = await txFn.query.users.findFirst({
                where: eq(users.id, BigInt(id)),
                with: { class: true }
            });

            if (!user) {
                await txFn.insert(users).values({
                    id: BigInt(id),
                    username,
                }).returning();

                // Re-query to get the user with class relation
                user = await txFn.query.users.findFirst({
                    where: eq(users.id, BigInt(id)),
                    with: { class: true }
                });
            }
            return user;
        }, tx);
    },
    getUserClass: async (id: string) => {
        const user = await DrizzleClient.query.users.findFirst({
            where: eq(users.id, BigInt(id)),
            with: { class: true }
        });
        return user?.class;
    },
    createUser: async (id: string | bigint, username: string, classId?: bigint, tx?: Transaction) => {
        return await withTransaction(async (txFn) => {
            const [user] = await txFn.insert(users).values({
                id: BigInt(id),
                username,
                classId,
            }).returning();
            return user;
        }, tx);
    },
    updateUser: async (id: string, data: Partial<typeof users.$inferInsert>, tx?: Transaction) => {
        return await withTransaction(async (txFn) => {
            const [user] = await txFn.update(users)
                .set(data)
                .where(eq(users.id, BigInt(id)))
                .returning();
            return user;
        }, tx);
    },
    deleteUser: async (id: string, tx?: Transaction) => {
        return await withTransaction(async (txFn) => {
            await txFn.delete(users).where(eq(users.id, BigInt(id)));
        }, tx);
    },
};