"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.DJANGO_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 fast_glob_1 = __importDefault(require("fast-glob"));
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
const utils_1 = require("./utils");
exports.DJANGO_AGENT_CONFIG = {
    metadata: {
        name: 'Django',
        glyph: '🎸',
        glyphColor: '#44B78B',
        integration: constants_1.Integration.django,
        beta: true,
        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.getDjangoProjectType)(options);
            const settingsFile = await (0, utils_1.findDjangoSettingsFile)(options);
            return { projectType, settingsFile };
        },
    },
    detection: {
        packageName: 'django',
        packageDisplayName: 'Django',
        usesPackageJson: false,
        getVersion: () => undefined,
        getVersionBucket: utils_1.getDjangoVersionBucket,
        minimumVersion: '3.0.0',
        getInstalledVersion: (options) => (0, utils_1.getDjangoVersion)(options),
        detect: async (options) => {
            const { installDir } = options;
            const managePyMatches = await (0, fast_glob_1.default)('**/manage.py', {
                cwd: installDir,
                ignore: utils_1.IGNORE_PATTERNS,
            });
            if (managePyMatches.length > 0) {
                for (const match of managePyMatches) {
                    try {
                        const content = fs.readFileSync(path.join(installDir, match), 'utf-8');
                        // Check for actual Django imports and usage
                        if (content.includes('from django') ||
                            content.includes('import django') ||
                            content.includes('DJANGO_SETTINGS_MODULE') ||
                            /execute_from_command_line/.test(content)) {
                            return true;
                        }
                    }
                    catch {
                        continue;
                    }
                }
            }
            const requirementsFiles = await (0, fast_glob_1.default)(['**/requirements*.txt', '**/pyproject.toml', '**/setup.py'], {
                cwd: installDir,
                ignore: utils_1.IGNORE_PATTERNS,
            });
            for (const reqFile of requirementsFiles) {
                try {
                    const content = fs.readFileSync(path.join(installDir, reqFile), 'utf-8');
                    // Match Django as a package requirement, not in comments or other text
                    // Look for: django, django>=, django==, django~=, Django (capitalized)
                    if (/^django([>=~!<\s]|$)/im.test(content) ||
                        /["']django["']/i.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) => ({
            projectType: context.projectType || 'unknown',
        }),
    },
    prompts: {
        packageInstallation: framework_config_1.PYTHON_PACKAGE_INSTALLATION,
        projectTypeDetection: 'This is a Python/Django project. Look for requirements.txt, pyproject.toml, setup.py, Pipfile, or manage.py to confirm.',
        getAdditionalContextLines: (context) => {
            const projectTypeName = context.projectType
                ? (0, utils_1.getDjangoProjectTypeName)(context.projectType)
                : 'unknown';
            // Map project type to framework ID for MCP docs resource
            const frameworkIdMap = {
                [utils_1.DjangoProjectType.STANDARD]: 'django',
                [utils_1.DjangoProjectType.DRF]: 'django',
                [utils_1.DjangoProjectType.WAGTAIL]: 'django',
                [utils_1.DjangoProjectType.CHANNELS]: 'django',
            };
            const frameworkId = context.projectType
                ? frameworkIdMap[context.projectType]
                : 'django';
            const lines = [
                `Project type: ${projectTypeName}`,
                `Framework docs ID: ${frameworkId} (use amplitude://docs/frameworks/${frameworkId} for documentation)`,
            ];
            if (context.settingsFile) {
                lines.push(`Settings file: ${context.settingsFile}`);
            }
            return lines;
        },
    },
    ui: {
        successMessage: 'Amplitude integration complete',
        estimatedDurationMinutes: 5,
        getOutroChanges: (context) => {
            const projectTypeName = context.projectType
                ? (0, utils_1.getDjangoProjectTypeName)(context.projectType)
                : 'Django';
            return [
                `Analyzed your ${projectTypeName} project structure`,
                `Installed the Amplitude Python package`,
                `Configured Amplitude in your Django settings`,
                `Added Amplitude middleware for event tracking`,
            ];
        },
        getOutroNextSteps: () => [
            'Start your Django development server to see Amplitude in action',
            'Visit your Amplitude dashboard to see incoming events',
            'Use amplitude.track() to capture events and amplitude.identify() for users',
        ],
    },
};
