import * as fs from 'fs'
import * as path from 'path'

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

/**
 * Export routes to JSON file
 */
export async function exportToJSON(
    routes: Route[],
    projectName: string,
    filePath: string,
    baseUrl?: string
): Promise<void> {
    const exportData = {
        projectName,
        timestamp: new Date().toISOString(),
        framework: 'Express',
        baseUrl,
        routes,
        metadata: {
            totalRoutes: routes.length,
            tags: Array.from(new Set(routes.flatMap(r => r.tags || []))),
            extractedAt: new Date().toISOString(),
        },
    }

    const fullPath = path.join(process.cwd(), filePath)

    try {
        fs.writeFileSync(fullPath, JSON.stringify(exportData, null, 2), 'utf-8')
        console.log(`💾 Saved API export to: ${fullPath}`)
    } catch (error: any) {
        console.error('Failed to save JSON:', error.message)
        throw error
    }
}
