import axios from 'axios'

export interface Route {
    method: string
    path: string
    description: string
    tags: string[]
    middleware: string[]
    parameters?: any[]
    responses: Record<string, any>
}

export interface UploadOptions {
    projectName: string
    baseUrl?: string
    apiForgeUrl?: string
    apiKey: string
}

/**
 * Upload routes to APIForge server
 */
export async function uploadRoutes(routes: Route[], options: UploadOptions): Promise<void> {
    const url = options.apiForgeUrl || process.env.APIFORGE_URL || 'http://localhost:3000'

    try {
        const response = await axios.post(
            `${url}/api/upload-routes`,
            {
                projectName: options.projectName,
                routes,
                baseUrl: options.baseUrl,
                framework: 'Express',
            },
            {
                headers: {
                    'Content-Type': 'application/json',
                    'X-API-Key': options.apiKey,
                },
                timeout: 30000,
            }
        )

        console.log('✅ Uploaded to APIForge:', response.data)
    } catch (error: any) {
        console.error('❌ Upload failed:', error.message)
        throw error
    }
}
