import api from './api';
import type {
  Category,
  Operation,
  AttributeDefinition,
  CreateListingDto
} from '../types';

export const wizardService = {
  getCategories: async (): Promise<Category[]> => {
    const response = await api.get<Category[]>('/categories');
    return response.data;
  },

  getOperations: async (categoryId: number): Promise<Operation[]> => {
    const response = await api.get<Operation[]>(`/categories/${categoryId}/operations`);
    return response.data;
  },

  getAttributes: async (categoryId: number): Promise<AttributeDefinition[]> => {
    const response = await api.get<AttributeDefinition[]>(`/attributedefinitions/category/${categoryId}`);
    return response.data;
  },

  createListing: async (data: CreateListingDto): Promise<{ id: number }> => {
    const response = await api.post<{ id: number }>('/listings', data);
    return response.data;
  },

  uploadImage: async (listingId: number, file: File): Promise<{ url: string }> => {
    const formData = new FormData();
    formData.append('file', file);
    const response = await api.post<{ url: string }>(`/images/upload/${listingId}`, formData, {
      headers: { 'Content-Type': 'multipart/form-data' }
    });
    return response.data;
  }
};