import { Router } from "express";

import { prisma } from "../db";
import { requireAuth } from "../middleware/auth";

export const userRouter = Router();
userRouter.use(requireAuth);

userRouter.get("/", async (req, res) => {
  const query = (req.query.query as string | undefined)?.trim();
  const users = await prisma.user.findMany({
    where: query
      ? {
          username: {
            contains: query,
            mode: "insensitive",
          },
        }
      : undefined,
    select: { id: true, username: true, avatar: true },
    orderBy: { username: "asc" },
  });
  return res.json(users);
});
