import type {
  CredentialValidators,
  ExecutionContext,
  ProviderExecutors,
  ProviderProxyExecutor,
  ProxyExecutionResult,
} from "../../core/types.ts";
import type { AircallActionContext } from "./runtime.ts";

import {
  createProviderProxyUrl,
  defineProviderExecutors,
  normalizeProviderProxyHeaders,
  ProviderRequestError,
  readProviderProxyErrorMessage,
  readProviderProxyResponse,
  requireApiKeyCredential,
  toProviderProxyError,
} from "../provider-runtime.ts";
import {
  aircallActionHandlers,
  aircallApiBaseUrl,
  buildAircallHeaders,
  resolveStoredAircallApiId,
  validateAircallCredential,
} from "./runtime.ts";

const service = "aircall";

export const executors: ProviderExecutors = defineProviderExecutors<AircallActionContext>({
  service,
  handlers: aircallActionHandlers,
  async createContext(context: ExecutionContext, fetcher: typeof fetch): Promise<AircallActionContext> {
    const credential = await requireApiKeyCredential(context, service);
    return {
      apiId: resolveStoredAircallApiId(credential.values, credential.metadata),
      apiKey: credential.apiKey,
      fetcher,
      signal: context.signal,
    };
  },
});

export const proxy: ProviderProxyExecutor = async (input, context): Promise<ProxyExecutionResult> => {
  try {
    const credential = await requireApiKeyCredential(context, service);
    const url = createProviderProxyUrl(aircallApiBaseUrl, input.endpoint, input.query);
    const headers = normalizeProviderProxyHeaders(input.headers);
    for (const [name, value] of Object.entries(
      buildAircallHeaders(resolveStoredAircallApiId(credential.values, credential.metadata), credential.apiKey),
    )) {
      headers.set(name, value);
    }
    if (input.body !== undefined && !headers.has("content-type") && typeof input.body !== "string") {
      headers.set("content-type", "application/json");
    }

    const response = await fetch(url, {
      method: input.method,
      headers,
      body:
        input.body === undefined ? undefined : typeof input.body === "string" ? input.body : JSON.stringify(input.body),
      signal: context.signal,
    });
    if (!response.ok) {
      const text = await readProviderProxyErrorMessage(response, "");
      throw new ProviderRequestError(response.status, text || `provider request failed with HTTP ${response.status}`);
    }
    return { ok: true, response: await readProviderProxyResponse(response) };
  } catch (error) {
    return toProviderProxyError(error, "provider request failed");
  }
};

export const credentialValidators: CredentialValidators = {
  apiKey(input, { fetcher, signal }) {
    return validateAircallCredential(input, fetcher, signal);
  },
};
