"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NLPController = void 0;
const tsyringe_1 = require("tsyringe");
const zod_1 = require("zod");
const nlp_service_1 = require("../../services/nlp/nlp.service");
const translation_service_1 = require("../../services/nlp/translation.service");
const language_detector_service_1 = require("../../services/nlp/language-detector.service");
/**
 * Request schemas for NLP endpoints
 */
const ParseRequestSchema = zod_1.z.object({
    text: zod_1.z.string().min(1).max(10000),
    language: zod_1.z.string().optional(),
    projectId: zod_1.z.string().optional(),
    userId: zod_1.z.string().optional(),
    context: zod_1.z.record(zod_1.z.any()).optional(),
});
const ExtractEntitiesRequestSchema = zod_1.z.object({
    text: zod_1.z.string().min(1).max(10000),
    language: zod_1.z.string().optional(),
    context: zod_1.z.record(zod_1.z.any()).optional(),
});
const ClassifyRequestSchema = zod_1.z.object({
    text: zod_1.z.string().min(1).max(10000),
    language: zod_1.z.string().optional(),
    entities: zod_1.z.object({
        title: zod_1.z.string().optional(),
        description: zod_1.z.string().optional(),
        priority: zod_1.z.enum(['critical', 'high', 'medium', 'low']).optional(),
        deadline: zod_1.z.union([zod_1.z.date(), zod_1.z.string()]).optional(),
        estimatedHours: zod_1.z.number().optional(),
        assignees: zod_1.z.array(zod_1.z.string()).optional(),
        dependencies: zod_1.z.array(zod_1.z.string()).optional(),
        tags: zod_1.z.array(zod_1.z.string()).optional(),
    }).optional(),
});
const TranslateRequestSchema = zod_1.z.object({
    text: zod_1.z.string().min(1).max(10000),
    sourceLanguage: zod_1.z.string().optional(),
    targetLanguage: zod_1.z.string(),
    preserveFormatting: zod_1.z.boolean().optional(),
    context: zod_1.z.string().optional(),
});
const DetectLanguageRequestSchema = zod_1.z.object({
    text: zod_1.z.string().min(1).max(5000),
});
/**
 * NLP Controller for natural language processing endpoints
 */
class NLPController {
    constructor() {
        this.nlpService = tsyringe_1.container.resolve(nlp_service_1.NLPService);
        this.translationService = tsyringe_1.container.resolve(translation_service_1.TranslationService);
        this.languageDetector = tsyringe_1.container.resolve(language_detector_service_1.LanguageDetectorService);
    }
    /**
     * POST /api/nlp/parse
     * Parse natural language text into structured task data
     */
    async parse(req, res) {
        try {
            const validated = ParseRequestSchema.parse(req.body);
            const result = await this.nlpService.processTextWithTranslation({
                text: validated.text,
                language: validated.language,
                projectId: validated.projectId,
                userId: validated.userId,
                context: validated.context,
            });
            res.json({
                success: result.success,
                data: {
                    entities: result.entities,
                    classification: result.classification,
                    structuredData: result.structuredData,
                    language: result.language,
                },
                metadata: {
                    processingTime: result.processingTime,
                    tokensUsed: result.tokensUsed,
                    errors: result.errors,
                },
            });
        }
        catch (error) {
            if (error instanceof zod_1.z.ZodError) {
                res.status(400).json({
                    success: false,
                    error: 'Validation Error',
                    details: error.issues,
                });
            }
            else {
                console.error('NLP parse error:', error);
                res.status(500).json({
                    success: false,
                    error: 'Failed to parse text',
                    message: error instanceof Error ? error.message : 'Unknown error',
                });
            }
        }
    }
    /**
     * POST /api/nlp/extract-entities
     * Extract entities from natural language text
     */
    async extractEntities(req, res) {
        try {
            const validated = ExtractEntitiesRequestSchema.parse(req.body);
            const result = await this.nlpService.processText({
                text: validated.text,
                language: validated.language,
                context: validated.context,
            });
            res.json({
                success: result.success,
                data: {
                    entities: result.entities,
                },
                metadata: {
                    language: result.language,
                    processingTime: result.processingTime,
                    confidence: result.entities.confidence,
                },
            });
        }
        catch (error) {
            if (error instanceof zod_1.z.ZodError) {
                res.status(400).json({
                    success: false,
                    error: 'Validation Error',
                    details: error.issues,
                });
            }
            else {
                console.error('Entity extraction error:', error);
                res.status(500).json({
                    success: false,
                    error: 'Failed to extract entities',
                    message: error instanceof Error ? error.message : 'Unknown error',
                });
            }
        }
    }
    /**
     * POST /api/nlp/classify
     * Classify task type and attributes
     */
    async classify(req, res) {
        try {
            const validated = ClassifyRequestSchema.parse(req.body);
            const result = await this.nlpService.processText({
                text: validated.text,
                language: validated.language,
                context: { entities: validated.entities },
            });
            res.json({
                success: result.success,
                data: {
                    classification: result.classification,
                },
                metadata: {
                    language: result.language,
                    processingTime: result.processingTime,
                    confidence: result.classification.confidence,
                },
            });
        }
        catch (error) {
            if (error instanceof zod_1.z.ZodError) {
                res.status(400).json({
                    success: false,
                    error: 'Validation Error',
                    details: error.issues,
                });
            }
            else {
                console.error('Classification error:', error);
                res.status(500).json({
                    success: false,
                    error: 'Failed to classify text',
                    message: error instanceof Error ? error.message : 'Unknown error',
                });
            }
        }
    }
    /**
     * POST /api/nlp/translate
     * Translate text between languages
     */
    async translate(req, res) {
        try {
            const validated = TranslateRequestSchema.parse(req.body);
            const result = await this.translationService.translate({
                text: validated.text,
                sourceLanguage: validated.sourceLanguage,
                targetLanguage: validated.targetLanguage,
                preserveFormatting: validated.preserveFormatting,
                context: validated.context,
            });
            res.json({
                success: true,
                data: {
                    originalText: result.originalText,
                    translatedText: result.translatedText,
                    sourceLanguage: result.sourceLanguage,
                    targetLanguage: result.targetLanguage,
                },
                metadata: {
                    confidence: result.confidence,
                    alternatives: result.alternatives,
                },
            });
        }
        catch (error) {
            if (error instanceof zod_1.z.ZodError) {
                res.status(400).json({
                    success: false,
                    error: 'Validation Error',
                    details: error.issues,
                });
            }
            else {
                console.error('Translation error:', error);
                res.status(500).json({
                    success: false,
                    error: 'Failed to translate text',
                    message: error instanceof Error ? error.message : 'Unknown error',
                });
            }
        }
    }
    /**
     * POST /api/nlp/detect-language
     * Detect the language of input text
     */
    async detectLanguage(req, res) {
        try {
            const validated = DetectLanguageRequestSchema.parse(req.body);
            const result = await this.languageDetector.detectLanguage(validated.text);
            res.json({
                success: true,
                data: {
                    language: result.language,
                    confidence: result.confidence,
                },
                metadata: {
                    alternatives: result.alternatives,
                },
            });
        }
        catch (error) {
            if (error instanceof zod_1.z.ZodError) {
                res.status(400).json({
                    success: false,
                    error: 'Validation Error',
                    details: error.issues,
                });
            }
            else {
                console.error('Language detection error:', error);
                res.status(500).json({
                    success: false,
                    error: 'Failed to detect language',
                    message: error instanceof Error ? error.message : 'Unknown error',
                });
            }
        }
    }
    /**
     * GET /api/nlp/languages
     * Get supported languages
     */
    async getSupportedLanguages(req, res) {
        try {
            const languages = this.languageDetector.getSupportedLanguages();
            res.json({
                success: true,
                data: {
                    languages,
                },
            });
        }
        catch (error) {
            console.error('Get languages error:', error);
            res.status(500).json({
                success: false,
                error: 'Failed to get supported languages',
                message: error instanceof Error ? error.message : 'Unknown error',
            });
        }
    }
    /**
     * GET /api/nlp/cache/stats
     * Get NLP cache statistics
     */
    async getCacheStats(req, res) {
        try {
            const nlpStats = this.nlpService.getCacheStats();
            const translationStats = this.translationService.getCacheStats();
            res.json({
                success: true,
                data: {
                    nlpCache: nlpStats,
                    translationCache: translationStats,
                },
            });
        }
        catch (error) {
            console.error('Get cache stats error:', error);
            res.status(500).json({
                success: false,
                error: 'Failed to get cache statistics',
                message: error instanceof Error ? error.message : 'Unknown error',
            });
        }
    }
    /**
     * DELETE /api/nlp/cache
     * Clear NLP caches
     */
    async clearCache(req, res) {
        try {
            this.nlpService.clearCache();
            this.translationService.clearCache();
            res.json({
                success: true,
                message: 'Cache cleared successfully',
            });
        }
        catch (error) {
            console.error('Clear cache error:', error);
            res.status(500).json({
                success: false,
                error: 'Failed to clear cache',
                message: error instanceof Error ? error.message : 'Unknown error',
            });
        }
    }
}
exports.NLPController = NLPController;
//# sourceMappingURL=nlp.controller.js.map