import "server-only";
import { and, asc, desc, eq } from "drizzle-orm";
import { db } from "@/lib/db/client";
import { teamMembers } from "@/lib/db/schema";

export async function listActiveTeamMembers() {
  return db
    .select()
    .from(teamMembers)
    .where(eq(teamMembers.status, "active"))
    .orderBy(asc(teamMembers.position));
}

export async function getActiveTeamMemberBySlug(slug: string) {
  const [row] = await db
    .select()
    .from(teamMembers)
    .where(and(eq(teamMembers.slug, slug), eq(teamMembers.status, "active")))
    .limit(1);
  return row ?? null;
}

export async function listTeamMembersForAdmin() {
  return db.select().from(teamMembers).orderBy(desc(teamMembers.createdAt));
}

export async function getTeamMemberForAdmin(id: string) {
  const [row] = await db
    .select()
    .from(teamMembers)
    .where(eq(teamMembers.id, id))
    .limit(1);
  return row ?? null;
}
