import { apiDelete, apiGet, apiPatch, apiPost, apiRequest } from "./client";

export interface FleetProviderModel {
  id: string;
  name?: string;
}

export interface FleetProvider {
  id: string;
  name: string;
  displayName: string;
  type: string;
  baseUrl: string | null;
  isActive: boolean;
  models: unknown;
}

export interface CreateFleetProviderRequest {
  name: string;
  displayName: string;
  type: string;
  baseUrl?: string;
  apiKey?: string;
  apiType?: string;
  models?: FleetProviderModel[];
}

export interface UpdateFleetProviderRequest {
  displayName?: string;
  baseUrl?: string;
  apiKey?: string;
  isActive?: boolean;
  models?: FleetProviderModel[];
}

export interface FleetAgentConfig {
  primaryModel: string | null;
  fallbackModels: string[];
  personality: string | null;
}

interface FleetAgentConfigResponse {
  primaryModel: string | null;
  fallbackModels: unknown[];
  personality: string | null;
}

export interface UpdateFleetAgentConfigRequest {
  primaryModel?: string;
  fallbackModels?: string[];
  personality?: string;
}

export interface FleetOidcConfig {
  issuerUrl?: string;
  clientId?: string;
  configured: boolean;
}

export interface UpdateFleetOidcConfigRequest {
  issuerUrl: string;
  clientId: string;
  clientSecret: string;
}

export interface ResetBreakglassAdminPasswordRequest {
  username: string;
  newPassword: string;
}

function normalizeStringArray(value: unknown[]): string[] {
  return value.filter((item): item is string => typeof item === "string");
}

export async function fetchFleetProviders(): Promise<FleetProvider[]> {
  return apiGet<FleetProvider[]>("/api/fleet-settings/providers");
}

export async function createFleetProvider(
  data: CreateFleetProviderRequest
): Promise<{ id: string }> {
  return apiPost<{ id: string }>("/api/fleet-settings/providers", data);
}

export async function updateFleetProvider(
  providerId: string,
  data: UpdateFleetProviderRequest
): Promise<void> {
  await apiPatch<unknown>(`/api/fleet-settings/providers/${providerId}`, data);
}

export async function deleteFleetProvider(providerId: string): Promise<void> {
  await apiDelete<unknown>(`/api/fleet-settings/providers/${providerId}`);
}

export async function fetchFleetAgentConfig(): Promise<FleetAgentConfig> {
  const response = await apiGet<FleetAgentConfigResponse>("/api/fleet-settings/agent-config");

  return {
    primaryModel: response.primaryModel,
    fallbackModels: normalizeStringArray(response.fallbackModels),
    personality: response.personality,
  };
}

export async function updateFleetAgentConfig(data: UpdateFleetAgentConfigRequest): Promise<void> {
  await apiPatch<unknown>("/api/fleet-settings/agent-config", data);
}

export async function fetchFleetOidcConfig(): Promise<FleetOidcConfig> {
  return apiGet<FleetOidcConfig>("/api/fleet-settings/oidc");
}

export async function updateFleetOidcConfig(data: UpdateFleetOidcConfigRequest): Promise<void> {
  await apiRequest<unknown>("/api/fleet-settings/oidc", {
    method: "PUT",
    body: JSON.stringify(data),
  });
}

export async function deleteFleetOidcConfig(): Promise<void> {
  await apiDelete<unknown>("/api/fleet-settings/oidc");
}

export async function resetBreakglassAdminPassword(
  data: ResetBreakglassAdminPasswordRequest
): Promise<void> {
  await apiPost<unknown>("/api/fleet-settings/breakglass/reset-password", data);
}
