import { API_BASE_URL } from '../utils/constants';

export interface ImportTask {
    id: string;
    sourcePath: string;
    targetGroupId?: string;
    targetGroupName?: string;
    scheduledAt?: string;
    status: 'PENDING' | 'PROCESSING' | 'COMPLETED' | 'FAILED';
    logs?: string;
    embeddingModelId?: string;
    chunkSize?: number;
    chunkOverlap?: number;
    mode?: string;
    createdAt: string;
}

export const importService = {
    create: async (token: string, data: {
        sourcePath: string;
        targetGroupId?: string;
        targetGroupName?: string;
        embeddingModelId: string;
        scheduledAt?: string;
        chunkSize?: number;
        chunkOverlap?: number;
        mode?: string;
    }): Promise<ImportTask> => {
        const response = await fetch(`${API_BASE_URL}/import-tasks`, {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        });
        if (!response.ok) throw new Error('Failed to create import task');
        return response.json();
    },

    getAll: async (token: string): Promise<ImportTask[]> => {
        const response = await fetch(`${API_BASE_URL}/import-tasks`, {
            headers: {
                'Authorization': `Bearer ${token}`
            }
        });
        if (!response.ok) throw new Error('Failed to fetch import tasks');
        return response.json();
    },

    delete: async (token: string, id: string): Promise<void> => {
        const response = await fetch(`${API_BASE_URL}/import-tasks/${id}`, {
            method: 'DELETE',
            headers: {
                'Authorization': `Bearer ${token}`
            }
        });
        if (!response.ok) throw new Error('Failed to delete import task');
    }
};
