import axios from 'axios';

const API_BASE_URL = process.env.REACT_APP_API_BASE_URL || process.env.REACT_APP_API_URL || 'http://localhost:8080/api/v1';

export interface ClothingItem {
  id: number;
  title: string;
  description?: string;
  price?: number;
  currency?: string;
  image_url: string;
  url?: string;
  is_active?: boolean;
  display_order?: number;
  created_at?: string;
  updated_at?: string;
}

export interface ClothingResponse {
  data: ClothingItem[];
}

// Public endpoint - get all active clothing items
export const getClothing = async (): Promise<ClothingItem[]> => {
  const response = await axios.get<ClothingResponse>(`${API_BASE_URL}/clothing`);
  return response.data.data;
};

// Admin endpoint - get all clothing items
export const getClothingAdmin = async (): Promise<ClothingItem[]> => {
  const response = await axios.get<ClothingResponse>(`${API_BASE_URL}/admin/clothing`, {
    withCredentials: true,
  });
  return response.data.data;
};

// Admin endpoint - create clothing item
export const createClothing = async (data: Partial<ClothingItem>): Promise<ClothingItem> => {
  const response = await axios.post<ClothingItem>(`${API_BASE_URL}/admin/clothing`, data, {
    withCredentials: true,
  });
  return response.data;
};

// Admin endpoint - update clothing item
export const updateClothing = async (id: number, data: Partial<ClothingItem>): Promise<ClothingItem> => {
  const response = await axios.put<ClothingItem>(`${API_BASE_URL}/admin/clothing/${id}`, data, {
    withCredentials: true,
  });
  return response.data;
};

// Admin endpoint - delete clothing item
export const deleteClothing = async (id: number): Promise<void> => {
  await axios.delete(`${API_BASE_URL}/admin/clothing/${id}`, {
    withCredentials: true,
  });
};

// Admin endpoint - update display order
export const updateClothingOrder = async (items: Array<{ id: number; display_order: number }>): Promise<void> => {
  await axios.post(`${API_BASE_URL}/admin/clothing/reorder`, items, {
    withCredentials: true,
  });
};
