import { builder } from "../builder";
import { StatsType } from "../types/index";
import { users, videos } from "../../db/schema/index";
import { eq, count } from "drizzle-orm";

builder.queryField("stats", (t) =>
  t.field({
    type: StatsType,
    resolve: async (_root, _args, ctx) => {
      const modelsCount = await ctx.db
        .select({ count: count() })
        .from(users)
        .where(eq(users.role, "model"));
      const viewersCount = await ctx.db
        .select({ count: count() })
        .from(users)
        .where(eq(users.role, "viewer"));
      const videosCount = await ctx.db.select({ count: count() }).from(videos);

      return {
        models_count: modelsCount[0]?.count || 0,
        viewers_count: viewersCount[0]?.count || 0,
        videos_count: videosCount[0]?.count || 0,
      };
    },
  }),
);
