import "server-only";
import { cache } from "react";
import { notFound } from "next/navigation";
import { db } from "./db";

/** Public conference site loader — only PUBLISHED/LIVE/COMPLETED conferences are visible. */
export const getPublicConference = cache(async (slug: string) => {
  const conf = await db.conference.findUnique({
    where: { slug },
    include: { org: true, sponsors: { orderBy: { amount: "desc" } }, tracks: true },
  });
  if (!conf || conf.status === "DRAFT" || conf.status === "ARCHIVED") notFound();
  return conf;
});

export function regState(conf: { regOpensAt: Date | null; regClosesAt: Date | null; status: string }) {
  const now = new Date();
  if (conf.status === "COMPLETED") return "closed" as const;
  if (conf.regOpensAt && now < conf.regOpensAt) return "not_open" as const;
  if (conf.regClosesAt && now > conf.regClosesAt) return "closed" as const;
  return "open" as const;
}
