import { apiClient } from "./client";

export interface ModelItem {
  id: string;
  name: string;
  checked: boolean;
}

export interface ModelProvider {
  id: number;
  name: string;
  protocol: string;
  capability: "text" | "image" | "video";
  baseUrl: string;
  apiKey: string;
  secretKey?: string;
  models: ModelItem[];
}

export interface ModelDefaults {
  text?: { providerId: number; modelId: string } | null;
  image?: { providerId: number; modelId: string } | null;
  video?: { providerId: number; modelId: string } | null;
}

export type CreateProviderPayload = Omit<ModelProvider, "id">;

export const modelProvidersApi = {
  list: async (capability?: string): Promise<ModelProvider[]> => {
    const r = await apiClient.get("/models/providers", {
      params: capability ? { capability } : {},
    });
    return r.data.data;
  },
  create: async (payload: CreateProviderPayload): Promise<ModelProvider> => {
    const r = await apiClient.post("/models/providers", payload);
    return r.data.data;
  },
  update: async (id: number, payload: Partial<CreateProviderPayload>): Promise<ModelProvider> => {
    const r = await apiClient.put(`/models/providers/${id}`, payload);
    return r.data.data;
  },
  delete: async (id: number): Promise<void> => {
    await apiClient.delete(`/models/providers/${id}`);
  },
  getDefaults: async (): Promise<ModelDefaults> => {
    const r = await apiClient.get("/models/defaults");
    return r.data.data;
  },
  saveDefaults: async (defaults: ModelDefaults): Promise<void> => {
    await apiClient.put("/models/defaults", defaults);
  },
};
