// src/lib/keywords/shortlist.ts — server-side read for the F8 shortlist (per-site cart).
import "server-only";
import { eq } from "drizzle-orm";
import { db } from "@/lib/db/client";
import { keywordShortlist, keywords } from "@/lib/db/domain-schema";

export interface ShortlistItem {
  id: string;
  term: string;
  country: string;
}

/** The site's shortlist, joined onto `keywords` for `term`/`country` — the
 *  UI renders the term, not a bare id (P1 réservoir refactor: was a bare
 *  `string[]`; `keyword_shortlist.keyword_id` cascades on delete, so the
 *  inner join never drops a row silently). */
export async function getShortlistForSite(
  siteId: string,
): Promise<ShortlistItem[]> {
  return db
    .select({
      id: keywordShortlist.keywordId,
      term: keywords.term,
      country: keywords.country,
    })
    .from(keywordShortlist)
    .innerJoin(keywords, eq(keywords.id, keywordShortlist.keywordId))
    .where(eq(keywordShortlist.siteId, siteId));
}
