import { create } from 'zustand';
import { api } from '../../services/api';

interface CanvasRequestData {
    endpoint: string;
    params?: Record<string, any>;
    courseId?: number;
}

type CanvasStore = {
    sendCanvasRequest: (data: CanvasRequestData) => Promise<any>;
};

export const useCanvasStore = create<CanvasStore>(() => ({
    sendCanvasRequest: async (requestData) => {
        try {
            if (!requestData.endpoint) {
                throw new Error("Invalid input: 'endpoint' is required.");
            }

            // Construct the full URL with course ID if provided
            const url = requestData.courseId
                ? `/courses/${requestData.courseId}${requestData.endpoint}`
                : requestData.endpoint;

            console.log('Sending Canvas request:', { url, params: requestData.params });
            const response = await api('get', url, undefined, requestData.params, 'json', 60, true);
            console.log('Got Canvas response:', response);
            return response.data;
        } catch (error) {
            console.error('Failed to send Canvas request:', error);
            throw error;
        }
    },
})); 