// @ts-nocheck

import { fetchJsonOrThrow, buildAdminHeaders } from "./utils";

export async function loadLauncherRequests() {
  return fetchJsonOrThrow("/api/launcher-requests");
}

export async function loadLauncherRequestMeta() {
  return fetchJsonOrThrow("/api/launcher-request-meta");
}

export async function createLauncherRequest(text) {
  return fetchJsonOrThrow("/api/launcher-requests", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ text }),
  });
}

export async function loadLauncherRecentSaveEvents(adminPassword) {
  return fetchJsonOrThrow("/api/debug/recent-saves", {
    headers: buildAdminHeaders(adminPassword),
  });
}

export async function verifyLauncherAdminPassword(password) {
  const payload = await fetchJsonOrThrow("/api/admin/auth-check", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ password }),
  });
  if (!payload.accessGranted) {
    throw new Error(String(payload.error || "Admin access denied."));
  }
  return payload;
}

export function buildLauncherAdminSavePayload(adminPassword, requestEntry) {
  return {
    method: "PATCH",
    headers: buildAdminHeaders(adminPassword, {
      "Content-Type": "application/json",
    }),
    body: JSON.stringify({
      title: requestEntry.title,
      status: requestEntry.status,
      category: requestEntry.category,
      tags: requestEntry.tags,
      sourceText: requestEntry.sourceText,
      summary: requestEntry.summary,
      implementationNotes: requestEntry.implementationNotes,
      analysis: requestEntry.analysis,
    }),
  };
}

export async function saveLauncherAdminRequest(adminPassword, requestEntry) {
  return fetchJsonOrThrow(
    `/api/launcher-requests/${encodeURIComponent(requestEntry.id)}`,
    buildLauncherAdminSavePayload(adminPassword, requestEntry),
  );
}

export async function promoteLauncherAdminRequest(adminPassword, requestEntry) {
  return fetchJsonOrThrow(
    `/api/launcher-requests/${encodeURIComponent(requestEntry.id)}/process-analysis`,
    {
      method: "POST",
      headers: buildAdminHeaders(adminPassword, {
        "Content-Type": "application/json",
      }),
      body: JSON.stringify({
        action: "promote",
        analysis: requestEntry.analysis,
      }),
    },
  );
}

export async function requeueLauncherAdminRequest(adminPassword, requestEntry, mode) {
  return fetchJsonOrThrow(
    `/api/launcher-requests/${encodeURIComponent(requestEntry.id)}/requeue-analysis`,
    {
      method: "POST",
      headers: buildAdminHeaders(adminPassword, {
        "Content-Type": "application/json",
      }),
      body: JSON.stringify({
        mode,
        request: mode === "draft"
          ? {
            title: requestEntry.title,
            category: requestEntry.category,
            tags: requestEntry.tags,
            sourceText: requestEntry.sourceText,
            summary: requestEntry.summary,
            implementationNotes: requestEntry.implementationNotes,
          }
          : undefined,
      }),
    },
  );
}

export async function deleteLauncherRequest(adminPassword, requestId) {
  return fetchJsonOrThrow(`/api/launcher-requests/${encodeURIComponent(requestId)}`, {
    method: "DELETE",
    headers: buildAdminHeaders(adminPassword),
  });
}

export async function triggerLauncherPendingQueue(adminPassword) {
  return fetchJsonOrThrow("/api/launcher-requests/process-pending", {
    method: "POST",
    headers: buildAdminHeaders(adminPassword),
  });
}
