import { getSetting, SettingKey } from "./settings";

type Instance = {
  name: string;
  version: string;
  port: number;
  status: string;
};

class api {


} export default api;

async function apiFetch(path: string, options: RequestInit = {}, errorContext: string): Promise<Response> {
  const token = getSetting(SettingKey.Token);
  const headers = {
    ...options.headers,
    ...(token ? { Authorization: `Bearer ${token}` } : {}),
  };

  const res = await fetch(`${getSetting(SettingKey.Address)}${path}`, { ...options, headers });
  if (!res.ok) {
    const message = await res.text();
    throw new Error(`${errorContext}: ${message.trim()}`);
  }
  return res;
}

// --- GET CALLS ---

export async function fetchInstanceList(): Promise<Instance[]> {
  const res = await apiFetch("/instances/list", {}, "Could not fetch instance list");
  return res.json();
}

export async function fetchInstanceLogs(name: string): Promise<string[]> {
  const res = await apiFetch(`/instances/logs?name=${name}`, {}, "Could not fetch instance logs");
  return res.json();
}

export async function fetchInstanceConfig(name: string): Promise<string> {
  const res = await apiFetch(`/instances/getConfig?name=${name}`, {}, "Could not fetch instance config");
  return res.text();
}

// --- POST CALLS ---

export async function sendStartInstanceRequest(name: string) {
  const res = await apiFetch(`/instances/start?name=${name}`, { method: "POST" }, "Start Instance Request failed");
  return res.text();
}

export async function sendStopInstanceRequest(name: string) {
  const res = await apiFetch(`/instances/stop?name=${name}`, { method: "POST" }, "Stop Instance Request failed");
  return res.text();
}

export async function sendCreateInstanceRequest(name: string, version: string, port: number) {
  const res = await apiFetch(`/instances/create?name=${name}&version=${version}&port=${port}`, { method: "POST" }, "Create instance request failed");
  return res.text();
}

export async function sendDeleteInstanceRequest(name: string, purge: boolean) {
  const res = await apiFetch(`/instances/delete?name=${name}&purge=${purge}`, { method: "POST" }, "Delete instance request failed");
  return res.text();
}

export async function sendModifyInstanceRequest(name: string, newName: string, newPort: number) {
  const res = await apiFetch(`/instances/modify?name=${name}&newName=${newName}&newPort=${newPort}`, { method: "POST" }, "Modify instance request failed");
  return res.text();
}

export async function sendCommandRequest(name: string, payload: string) {
  const res = await apiFetch(`/instances/command?name=${name}`, {
    method: "POST",
    body: JSON.stringify({ command: payload }),
    headers: { "Content-Type": "application/json" },
  }, "Send Instance Command failed");
  return res.text();
}

export async function sendSetInstanceConfigRequest(name: string, config: string) {
  const res = await apiFetch(`/instances/setConfig?name=${name}`, {
    method: "POST",
    body: config,
    headers: { "Content-Type": "application/json" },
  }, "Set instance config request failed");
  return res.text();
}