import { desc, eq } from "drizzle-orm";
import { createTRPCRouter, publicProcedure } from "../trpc";
import { articles as articlesTable } from "@/server/db/schema";

export const appRouter = createTRPCRouter({
  getSidebarMain: publicProcedure.query(async ({ ctx }) => {
    const categories = await ctx.db.query.categories.findMany({
      limit: 3,
      columns: {
        slug: true,
        name: true,
      },
    });
    const articles = await ctx.db.query.articles.findMany({
      limit: 3,
      where: eq(articlesTable.published, true),
      orderBy: desc(articlesTable.createdAt),
      columns: {
        slug: true,
        title: true,
      },
    });
    return {
      categories,
      articles,
    };
  }),
});
