import api from './api';

export type SupplyCategory =
    | 'FILTER'
    | 'CLEANING'
    | 'PPE'
    | 'OFFICE'
    | 'BATHROOM'
    | 'KITCHEN'
    | 'MAINTENANCE'
    | 'OTHER';

export interface SupplyItem {
    id: string;
    name: string;
    category: SupplyCategory;
    quantity: number;
    minThreshold: number;
    unit: string;
    location?: string;
    vendor?: string;
    productUrl?: string;
    lastOrdered?: string;
    notes?: string;
    createdAt: string;
    updatedAt: string;
}

export interface CreateSupplyItemData {
    name: string;
    category: SupplyCategory;
    quantity: number;
    minThreshold: number;
    unit: string;
    location?: string;
    vendor?: string;
    productUrl?: string;
    notes?: string;
}

export type UpdateSupplyItemData = Partial<CreateSupplyItemData>;

export const suppliesApi = {
    getAll: async () => {
        const response = await api.get<SupplyItem[]>('/supplies');
        return response.data;
    },

    getShoppingList: async () => {
        const response = await api.get<SupplyItem[]>('/supplies/shopping-list');
        return response.data;
    },

    getOne: async (id: string) => {
        const response = await api.get<SupplyItem>(`/supplies/${id}`);
        return response.data;
    },

    create: async (data: CreateSupplyItemData) => {
        const response = await api.post<SupplyItem>('/supplies', data);
        return response.data;
    },

    update: async (id: string, data: UpdateSupplyItemData) => {
        const response = await api.patch<SupplyItem>(`/supplies/${id}`, data);
        return response.data;
    },

    delete: async (id: string) => {
        await api.delete(`/supplies/${id}`);
    },

    markOrdered: async (id: string) => {
        const response = await api.post<SupplyItem>(`/supplies/${id}/order`);
        return response.data;
    },

    adjustQuantity: async (id: string, adjustment: number) => {
        const response = await api.post<SupplyItem>(`/supplies/${id}/adjust`, { adjustment });
        return response.data;
    },
};
