"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FASTAPI_AGENT_CONFIG = void 0;
const framework_config_1 = require("../../lib/framework-config");
const package_manager_detection_1 = require("../../lib/package-manager-detection");
const constants_1 = require("../../lib/constants");
const utils_1 = require("./utils");
const fast_glob_1 = __importDefault(require("fast-glob"));
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
/**
 * FastAPI framework configuration for the universal agent runner
 */
exports.FASTAPI_AGENT_CONFIG = {
    metadata: {
        name: 'FastAPI',
        glyph: '⚡',
        glyphColor: '#009688',
        integration: constants_1.Integration.fastapi,
        docsUrl: 'https://amplitude.com/docs/sdks/analytics/python',
        unsupportedVersionDocsUrl: 'https://amplitude.com/docs/sdks/analytics/python',
        gatherContext: async (options) => {
            const projectType = await (0, utils_1.getFastAPIProjectType)(options);
            const appFile = await (0, utils_1.findFastAPIAppFile)(options);
            return { projectType, appFile };
        },
    },
    detection: {
        packageName: 'fastapi',
        packageDisplayName: 'FastAPI',
        usesPackageJson: false,
        getVersion: (_packageJson) => {
            // For FastAPI, we don't use package.json. Version is extracted separately
            // from requirements.txt or pyproject.toml in the wizard entry point
            return undefined;
        },
        getVersionBucket: utils_1.getFastAPIVersionBucket,
        getInstalledVersion: utils_1.getFastAPIVersion,
        detect: async (options) => {
            const { installDir } = options;
            // Note: Django and Flask are checked before FastAPI in INTEGRATION_ORDER,
            // so if we get here, the project is not a Django or Flask project.
            // Check for FastAPI in requirements files
            const requirementsFiles = await (0, fast_glob_1.default)([
                '**/requirements*.txt',
                '**/pyproject.toml',
                '**/setup.py',
                '**/Pipfile',
            ], {
                cwd: installDir,
                ignore: utils_1.IGNORE_PATTERNS,
            });
            for (const reqFile of requirementsFiles) {
                try {
                    const content = fs.readFileSync(path.join(installDir, reqFile), 'utf-8');
                    // Check for fastapi package (case-insensitive)
                    // Match "fastapi" as a standalone package
                    if (/^fastapi([<>=~!]|$|\s)/im.test(content) ||
                        /["']fastapi["']/i.test(content)) {
                        return true;
                    }
                }
                catch {
                    continue;
                }
            }
            // Check for FastAPI app patterns in Python files
            const pyFiles = await (0, fast_glob_1.default)(['**/main.py', '**/app.py', '**/application.py', '**/__init__.py'], {
                cwd: installDir,
                ignore: utils_1.IGNORE_PATTERNS,
            });
            for (const pyFile of pyFiles) {
                try {
                    const content = fs.readFileSync(path.join(installDir, pyFile), 'utf-8');
                    if (content.includes('from fastapi import') ||
                        content.includes('import fastapi') ||
                        /FastAPI\s*\(/.test(content)) {
                        return true;
                    }
                }
                catch {
                    continue;
                }
            }
            return false;
        },
        detectPackageManager: package_manager_detection_1.detectPythonPackageManagers,
    },
    environment: {
        uploadToHosting: false,
        getEnvVars: (apiKey, _host) => ({
            AMPLITUDE_API_KEY: apiKey,
        }),
    },
    analytics: {
        getTags: (context) => {
            const projectType = context.projectType;
            return {
                projectType: projectType || 'unknown',
            };
        },
    },
    prompts: {
        packageInstallation: framework_config_1.PYTHON_PACKAGE_INSTALLATION,
        projectTypeDetection: 'This is a Python/FastAPI project. Look for requirements.txt, pyproject.toml, setup.py, Pipfile, or main.py/app.py to confirm.',
        getAdditionalContextLines: (context) => {
            const projectType = context.projectType;
            const projectTypeName = projectType
                ? (0, utils_1.getFastAPIProjectTypeName)(projectType)
                : 'unknown';
            // Map project type to framework ID for MCP docs resource
            const frameworkIdMap = {
                [utils_1.FastAPIProjectType.STANDARD]: 'fastapi',
                [utils_1.FastAPIProjectType.ROUTER]: 'fastapi',
                [utils_1.FastAPIProjectType.FULLSTACK]: 'fastapi',
            };
            const frameworkId = projectType ? frameworkIdMap[projectType] : 'fastapi';
            const lines = [
                `Project type: ${projectTypeName}`,
                `Framework docs ID: ${frameworkId} (use amplitude://docs/frameworks/${frameworkId} for documentation)`,
            ];
            if (context.appFile) {
                lines.push(`App file: ${String(context.appFile)}`);
            }
            return lines;
        },
    },
    ui: {
        successMessage: 'Amplitude integration complete',
        estimatedDurationMinutes: 5,
        getOutroChanges: (context) => {
            const projectType = context.projectType;
            const projectTypeName = projectType
                ? (0, utils_1.getFastAPIProjectTypeName)(projectType)
                : 'FastAPI';
            return [
                `Analyzed your ${projectTypeName} project structure`,
                `Installed the Amplitude Python package`,
                `Configured Amplitude in your FastAPI application`,
                `Added Amplitude initialization with lifespan event handling`,
            ];
        },
        getOutroNextSteps: () => [
            'Start your FastAPI development server to see Amplitude in action',
            'Visit your Amplitude dashboard to see incoming events',
            'Use amplitude.identify() to associate events with users',
        ],
    },
};
