import { apiClient } from "./client";

export interface GlobalAsset {
  id: string;
  creatorUserId: string;
  name: string;
  assetType: "character" | "scene" | "prop";
  libraryType: "personal" | "team";
  tags: string[];
  imageUrl?: string;
  imageTosKey?: string;
  description?: string;
  favorite: boolean;
  createdAt: string;
}

export interface CreateAssetPayload {
  name: string;
  assetType: string;
  libraryType?: string;
  tags?: string;
  imageUrl?: string;
  imageTosKey?: string;
  description?: string;
  sourceProjectId?: string;
}

export const assetsApi = {
  list: async (type?: string, library = "personal"): Promise<GlobalAsset[]> => {
    const r = await apiClient.get("/assets", { params: { type, library } });
    return r.data.data ?? [];
  },
  create: async (payload: CreateAssetPayload): Promise<GlobalAsset> => {
    const r = await apiClient.post("/assets", payload);
    return r.data.data;
  },
  toggleFavorite: async (id: string): Promise<GlobalAsset> => {
    const r = await apiClient.put(`/assets/${id}/favorite`);
    return r.data.data;
  },
  update: async (id: string, patch: Partial<CreateAssetPayload>): Promise<GlobalAsset> => {
    const r = await apiClient.put(`/assets/${id}`, patch);
    return r.data.data;
  },
  delete: async (id: string): Promise<void> => {
    await apiClient.delete(`/assets/${id}`);
  },
};
