import api from './client';

export interface RenderTemplate {
  id: string;
  name: string;
  category_key: string | null;
  output_type_id: string | null;
  output_type_name: string | null;
  output_type_ids: string[];
  output_type_names: string[];
  blend_file_path: string;
  original_filename: string;
  target_collection: string;
  material_replace_enabled: boolean;
  lighting_only: boolean;
  shadow_catcher_enabled: boolean;
  camera_orbit: boolean;
  is_active: boolean;
  created_at: string;
  updated_at: string;
}

export interface MaterialLibraryInfo {
  exists: boolean;
  filename: string | null;
  size_bytes: number | null;
  path: string | null;
}

export async function listRenderTemplates(): Promise<RenderTemplate[]> {
  const { data } = await api.get<RenderTemplate[]>('/render-templates');
  return data;
}

export async function createRenderTemplate(formData: FormData): Promise<RenderTemplate> {
  const { data } = await api.post<RenderTemplate>('/render-templates', formData, {
    headers: { 'Content-Type': 'multipart/form-data' },
  });
  return data;
}

export async function duplicateRenderTemplate(
  sourceId: string,
  overrides: Partial<Pick<RenderTemplate, 'name' | 'category_key' | 'target_collection' | 'material_replace_enabled' | 'lighting_only' | 'shadow_catcher_enabled' | 'camera_orbit'>> & { output_type_ids?: string[] },
): Promise<RenderTemplate> {
  const fd = new FormData();
  fd.append('name', overrides.name || 'Untitled (copy)');
  fd.append('clone_blend_from', sourceId);
  fd.append('category_key', overrides.category_key || '');
  fd.append('output_type_ids', (overrides.output_type_ids || []).join(','));
  fd.append('target_collection', overrides.target_collection || 'Product');
  fd.append('material_replace_enabled', String(overrides.material_replace_enabled ?? false));
  fd.append('lighting_only', String(overrides.lighting_only ?? false));
  fd.append('shadow_catcher_enabled', String(overrides.shadow_catcher_enabled ?? false));
  fd.append('camera_orbit', String(overrides.camera_orbit ?? true));
  const { data } = await api.post<RenderTemplate>('/render-templates', fd, {
    headers: { 'Content-Type': 'multipart/form-data' },
  });
  return data;
}

export async function updateRenderTemplate(
  id: string,
  updates: Partial<Pick<RenderTemplate, 'name' | 'category_key' | 'output_type_ids' | 'target_collection' | 'material_replace_enabled' | 'lighting_only' | 'shadow_catcher_enabled' | 'camera_orbit' | 'is_active'>>,
): Promise<RenderTemplate> {
  const { data } = await api.patch<RenderTemplate>(`/render-templates/${id}`, updates);
  return data;
}

export async function deleteRenderTemplate(id: string): Promise<void> {
  await api.delete(`/render-templates/${id}`);
}

export async function reuploadBlendFile(id: string, file: File): Promise<RenderTemplate> {
  const fd = new FormData();
  fd.append('file', file);
  const { data } = await api.post<RenderTemplate>(`/render-templates/${id}/upload`, fd, {
    headers: { 'Content-Type': 'multipart/form-data' },
  });
  return data;
}

export async function uploadMaterialLibrary(file: File): Promise<MaterialLibraryInfo> {
  const fd = new FormData();
  fd.append('file', file);
  const { data } = await api.post<MaterialLibraryInfo>('/admin/settings/material-library', fd, {
    headers: { 'Content-Type': 'multipart/form-data' },
  });
  return data;
}

export async function getMaterialLibraryInfo(): Promise<MaterialLibraryInfo> {
  const { data } = await api.get<MaterialLibraryInfo>('/admin/settings/material-library');
  return data;
}

export async function deleteMaterialLibrary(): Promise<void> {
  await api.delete('/admin/settings/material-library');
}
