import { fetchApi, ApiResponse } from './api';

// Типы для категорий и коллекций
export interface Category {
  id: number;
  name: string;
  slug: string;
  description?: string | null;
  parent_id?: number | null;
  is_active: boolean;
  subcategories?: Category[];
  created_at?: string;
  updated_at?: string;
}

export interface Collection {
  id: number;
  name: string;
  slug: string;
  description?: string | null;
  is_active: boolean;
  created_at?: string;
  updated_at?: string;
}

// API функции для работы с категориями
export async function fetchCategories(): Promise<ApiResponse<Category[]>> {
  return fetchApi<Category[]>('/catalog/categories');
}

export async function fetchCategory(id: number): Promise<ApiResponse<Category>> {
  return fetchApi<Category>(`/catalog/categories/${id}`);
}

export async function createCategory(data: Partial<Category>): Promise<ApiResponse<Category>> {
  return fetchApi<Category>('/catalog/categories', {
    method: 'POST',
    body: JSON.stringify(data),
  });
}

export async function updateCategory(id: number, data: Partial<Category>): Promise<ApiResponse<Category>> {
  return fetchApi<Category>(`/catalog/categories/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data),
  });
}

export async function deleteCategory(id: number): Promise<ApiResponse<void>> {
  return fetchApi<void>(`/catalog/categories/${id}`, {
    method: 'DELETE',
  });
}

// API функции для работы с коллекциями
export async function fetchCollections(): Promise<ApiResponse<Collection[]>> {
  return fetchApi<Collection[]>('/catalog/collections');
}

export async function fetchCollection(id: number): Promise<ApiResponse<Collection>> {
  return fetchApi<Collection>(`/catalog/collections/${id}`);
}

export async function createCollection(data: Partial<Collection>): Promise<ApiResponse<Collection>> {
  return fetchApi<Collection>('/catalog/collections', {
    method: 'POST',
    body: JSON.stringify(data),
  });
}

export async function updateCollection(id: number, data: Partial<Collection>): Promise<ApiResponse<Collection>> {
  return fetchApi<Collection>(`/catalog/collections/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data),
  });
}

export async function deleteCollection(id: number): Promise<ApiResponse<void>> {
  return fetchApi<void>(`/catalog/collections/${id}`, {
    method: 'DELETE',
  });
}

// Экспортируем все API-функции
export default {
  fetchCategories,
  fetchCategory,
  createCategory,
  updateCategory,
  deleteCategory,
  fetchCollections,
  fetchCollection,
  createCollection,
  updateCollection,
  deleteCollection,
}; 