import { usePrisma } from "../utils/prisma";

export type CuratedDomainStatus = "suggested" | "approved" | "rejected";

/**
 * User schlägt eine Domain für die Country-Curated-Layer-2-Liste vor.
 * Wirft 409 wenn domain+country bereits existiert (egal welcher Status).
 */
export async function suggestCuratedDomain(
  suggestedByUserId: string,
  domain: string,
  country: string,
) {
  const db = usePrisma();

  // Existiert bereits? Statusabhängige Antwort
  const existing = await db.curatedDomain.findUnique({
    where: { country_domain: { country, domain } },
    select: { id: true, status: true },
  });

  if (existing) {
    // Bereits approved → User muss wissen dass es schon aktiv ist
    if (existing.status === "approved") {
      return { id: existing.id, domain, country, alreadyApproved: true };
    }
    // Bereits suggested oder rejected → idempotent zurückgeben
    return { id: existing.id, domain, country, alreadySuggested: true };
  }

  const row = await db.curatedDomain.create({
    data: {
      domain,
      country,
      status: "suggested",
      suggestedByUserId,
    },
    select: { id: true, domain: true, country: true, status: true, createdAt: true },
  });

  return row;
}

/**
 * Holt alle CuratedDomain-Einträge für die Admin-Inbox.
 * Ohne status-Filter: alle. Mit status="suggested" → nur offene Vorschläge.
 */
export async function getCuratedDomains(
  filters: { status?: CuratedDomainStatus; country?: string } = {},
) {
  const db = usePrisma();
  return db.curatedDomain.findMany({
    where: {
      ...(filters.status ? { status: filters.status } : {}),
      ...(filters.country ? { country: filters.country } : {}),
    },
    orderBy: [{ status: "asc" }, { createdAt: "asc" }],
    select: {
      id: true,
      domain: true,
      country: true,
      status: true,
      suggestedByUserId: true,
      createdAt: true,
      reviewedAt: true,
    },
  });
}

/**
 * Admin: Domain-Vorschlag genehmigen oder ablehnen.
 * reviewNote ist optional (für Reject-Begründung).
 */
export async function decideCuratedDomain(
  id: string,
  decision: "approved" | "rejected",
  reviewNote?: string,
) {
  const db = usePrisma();

  const existing = await db.curatedDomain.findUnique({
    where: { id },
    select: { id: true, status: true, domain: true, country: true },
  });

  if (!existing) {
    throw Object.assign(new Error("CuratedDomain not found"), { code: "NOT_FOUND" });
  }

  if (existing.status !== "suggested") {
    throw Object.assign(
      new Error("Domain already decided"),
      { code: "ALREADY_DECIDED", currentStatus: existing.status },
    );
  }

  const updated = await db.curatedDomain.update({
    where: { id },
    data: {
      status: decision,
      reviewedAt: new Date(),
    },
    select: { id: true, domain: true, country: true, status: true, reviewedAt: true },
  });

  return updated;
}
