"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.aiController = exports.AIController = void 0;
const zod_1 = require("zod");
const tsyringe_1 = require("tsyringe");
/**
 * Request validation schemas
 */
const CreateTaskSchema = zod_1.z.object({
    prompt: zod_1.z.string().min(1).max(1000),
    projectId: zod_1.z.string().optional(),
    context: zod_1.z.object({
        currentTasks: zod_1.z.array(zod_1.z.string()).optional(),
        completedTasks: zod_1.z.array(zod_1.z.string()).optional(),
        projectDescription: zod_1.z.string().optional(),
        userRole: zod_1.z.string().optional(),
    }).optional(),
});
const UpdateContextSchema = zod_1.z.object({
    feedback: zod_1.z.string().min(1).max(5000),
});
const BreakdownTaskSchema = zod_1.z.object({
    taskDescription: zod_1.z.string().min(1).max(2000),
    context: zod_1.z.string().optional(),
});
const GetSuggestionsSchema = zod_1.z.object({
    projectDescription: zod_1.z.string().min(1),
    completedTasks: zod_1.z.array(zod_1.z.string()),
    currentTasks: zod_1.z.array(zod_1.z.string()),
    goals: zod_1.z.array(zod_1.z.string()),
});
/**
 * Controller for AI-powered task management endpoints
 */
class AIController {
    constructor() {
        // Get services from DI container
        this.aiTaskService = tsyringe_1.container.resolve('AITaskService');
        this.openAIService = tsyringe_1.container.resolve('OpenAIService');
        this.langChainService = tsyringe_1.container.resolve('LangChainService');
    }
    /**
     * POST /api/ai/create-task
     * Create a task from natural language input
     */
    async createTask(req, res, next) {
        try {
            // Validate request body
            const validatedData = CreateTaskSchema.parse(req.body);
            // Create task using AI service
            const task = await this.aiTaskService.createTaskFromNaturalLanguage({
                prompt: validatedData.prompt,
                projectId: validatedData.projectId,
                context: validatedData.context,
            });
            res.status(201).json({
                success: true,
                data: task,
                message: 'Task created successfully from natural language',
            });
        }
        catch (error) {
            if (error instanceof zod_1.z.ZodError) {
                res.status(400).json({
                    success: false,
                    error: 'Validation error',
                    details: error.issues,
                });
                return;
            }
            // Check for rate limiting error
            if (error instanceof Error && error.message.includes('Rate limit exceeded')) {
                res.status(429).json({
                    success: false,
                    error: 'Rate limit exceeded',
                    message: 'Too many requests. Please try again later.',
                });
                return;
            }
            next(error);
        }
    }
    /**
     * PUT /api/ai/update-context/:taskId
     * Update AI context for a task with user feedback
     */
    async updateTaskContext(req, res, next) {
        try {
            const { taskId } = req.params;
            // Validate request body
            const validatedData = UpdateContextSchema.parse(req.body);
            // Update context
            await this.aiTaskService.updateTaskContext(taskId, validatedData.feedback);
            res.status(200).json({
                success: true,
                message: 'Task context updated successfully',
            });
        }
        catch (error) {
            if (error instanceof zod_1.z.ZodError) {
                res.status(400).json({
                    success: false,
                    error: 'Validation error',
                    details: error.issues,
                });
                return;
            }
            next(error);
        }
    }
    /**
     * GET /api/ai/suggestions/:projectId
     * Get AI-powered task suggestions for a project
     */
    async getTaskSuggestions(req, res, next) {
        try {
            const { projectId } = req.params;
            // Validate query parameters
            const validatedData = GetSuggestionsSchema.parse({
                projectDescription: req.query.projectDescription,
                completedTasks: req.query.completedTasks
                    ? req.query.completedTasks.split(',')
                    : [],
                currentTasks: req.query.currentTasks
                    ? req.query.currentTasks.split(',')
                    : [],
                goals: req.query.goals
                    ? req.query.goals.split(',')
                    : [],
            });
            // Get suggestions
            const suggestions = await this.aiTaskService.getTaskSuggestions(projectId, validatedData);
            res.status(200).json({
                success: true,
                data: suggestions,
                projectId,
            });
        }
        catch (error) {
            if (error instanceof zod_1.z.ZodError) {
                res.status(400).json({
                    success: false,
                    error: 'Validation error',
                    details: error.issues,
                });
                return;
            }
            // Check for rate limiting error
            if (error instanceof Error && error.message.includes('Rate limit exceeded')) {
                res.status(429).json({
                    success: false,
                    error: 'Rate limit exceeded',
                    message: 'Too many requests. Please try again later.',
                });
                return;
            }
            next(error);
        }
    }
    /**
     * POST /api/ai/breakdown/:taskId
     * Break down a task into subtasks using AI
     */
    async breakdownTask(req, res, next) {
        try {
            const { taskId } = req.params;
            // Validate request body
            const validatedData = BreakdownTaskSchema.parse(req.body);
            // Break down the task
            const breakdown = await this.aiTaskService.breakdownTask(taskId, validatedData.taskDescription, validatedData.context);
            res.status(200).json({
                success: true,
                data: breakdown,
                taskId,
            });
        }
        catch (error) {
            if (error instanceof zod_1.z.ZodError) {
                res.status(400).json({
                    success: false,
                    error: 'Validation error',
                    details: error.issues,
                });
                return;
            }
            // Check for rate limiting error
            if (error instanceof Error && error.message.includes('Rate limit exceeded')) {
                res.status(429).json({
                    success: false,
                    error: 'Rate limit exceeded',
                    message: 'Too many requests. Please try again later.',
                });
                return;
            }
            next(error);
        }
    }
    /**
     * GET /api/ai/health
     * Check health status of AI services
     */
    async getHealthStatus(req, res, next) {
        try {
            const health = await this.aiTaskService.getHealthStatus();
            const overallHealth = health.openai && health.langchain;
            res.status(overallHealth ? 200 : 503).json({
                success: true,
                healthy: overallHealth,
                services: health,
                timestamp: new Date().toISOString(),
            });
        }
        catch (error) {
            next(error);
        }
    }
    /**
     * GET /api/ai/usage
     * Get usage statistics for AI services
     */
    async getUsageStats(req, res, next) {
        try {
            const stats = await this.aiTaskService.getUsageStats();
            res.status(200).json({
                success: true,
                data: stats,
                timestamp: new Date().toISOString(),
            });
        }
        catch (error) {
            next(error);
        }
    }
    /**
     * POST /api/ai/warm-cache
     * Warm cache with common queries for a project
     */
    async warmCache(req, res, next) {
        try {
            const { projectId } = req.body;
            if (!projectId) {
                res.status(400).json({
                    success: false,
                    error: 'Project ID is required',
                });
                return;
            }
            // Warm cache in background
            this.aiTaskService.warmCache(projectId).catch(error => {
                console.error('Cache warming failed:', error);
            });
            res.status(202).json({
                success: true,
                message: 'Cache warming initiated',
                projectId,
            });
        }
        catch (error) {
            next(error);
        }
    }
    /**
     * POST /api/ai/test-connection
     * Test connection to AI services
     */
    async testConnection(req, res, next) {
        try {
            const [openaiTest, langchainTest] = await Promise.all([
                this.openAIService.testConnection(),
                this.langChainService.testConnection(),
            ]);
            res.status(200).json({
                success: true,
                connections: {
                    openai: openaiTest,
                    langchain: langchainTest,
                },
                message: openaiTest && langchainTest
                    ? 'All AI services are connected'
                    : 'Some AI services are not available',
            });
        }
        catch (error) {
            next(error);
        }
    }
}
exports.AIController = AIController;
// Export singleton instance
exports.aiController = new AIController();
//# sourceMappingURL=ai.controller.js.map