"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.chatWithAssistant = exports.getBusinessAnalytics = exports.optimizeImage = exports.generateAutoTags = exports.generateFaqs = exports.generateKeywords = exports.generateSeo = exports.generateDescription = exports.getSearchSuggestions = exports.smartSearch = exports.getTrendingRecommendations = exports.getPersonalizedRecommendations = exports.getSimilarBooks = void 0;
const ai_recommendations_service_1 = require("../services/ai-recommendations.service");
const ai_search_service_1 = require("../services/ai-search.service");
const ai_generator_service_1 = require("../services/ai-generator.service");
const ai_analytics_service_1 = require("../services/ai-analytics.service");
const ai_chat_service_1 = require("../services/ai-chat.service");
const image_optimizer_service_1 = require("../services/image-optimizer.service");
const response_utils_1 = require("../utils/response.utils");
// ==========================================
// 1. RECOMMENDATIONS
// ==========================================
const getSimilarBooks = async (req, res) => {
    try {
        const bookId = req.params.bookId;
        const limit = req.query.limit ? parseInt(req.query.limit, 10) : 6;
        const recommendations = await ai_recommendations_service_1.AIRecommendationsService.getSimilarBooks(bookId, limit);
        return (0, response_utils_1.sendSuccess)(res, 'Similar book recommendations retrieved', { recommendations });
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to retrieve recommendations', error.message, 500);
    }
};
exports.getSimilarBooks = getSimilarBooks;
const getPersonalizedRecommendations = async (req, res) => {
    try {
        const userId = req.user?.userId || req.query.userId;
        const limit = req.query.limit ? parseInt(req.query.limit, 10) : 8;
        if (!userId) {
            const trending = await ai_recommendations_service_1.AIRecommendationsService.getTrendingAIRecommendations(limit);
            return (0, response_utils_1.sendSuccess)(res, 'Trending AI recommendations retrieved', { recommendations: trending });
        }
        const recommendations = await ai_recommendations_service_1.AIRecommendationsService.getPersonalizedRecommendations(userId, limit);
        return (0, response_utils_1.sendSuccess)(res, 'Personalized recommendations retrieved', { recommendations });
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to retrieve recommendations', error.message, 500);
    }
};
exports.getPersonalizedRecommendations = getPersonalizedRecommendations;
const getTrendingRecommendations = async (req, res) => {
    try {
        const limit = req.query.limit ? parseInt(req.query.limit, 10) : 8;
        const recommendations = await ai_recommendations_service_1.AIRecommendationsService.getTrendingAIRecommendations(limit);
        return (0, response_utils_1.sendSuccess)(res, 'Trending AI recommendations retrieved', { recommendations });
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to retrieve trending recommendations', error.message, 500);
    }
};
exports.getTrendingRecommendations = getTrendingRecommendations;
// ==========================================
// 2. SMART SEARCH & SUGGESTIONS
// ==========================================
const smartSearch = async (req, res) => {
    try {
        const query = (req.query.q || req.body.query || '');
        const page = req.query.page ? parseInt(req.query.page, 10) : 1;
        const limit = req.query.limit ? parseInt(req.query.limit, 10) : 20;
        const result = await ai_search_service_1.AISearchService.smartSearch(query, page, limit);
        return (0, response_utils_1.sendSuccess)(res, 'AI Smart Search results retrieved', result);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to perform smart search', error.message, 500);
    }
};
exports.smartSearch = smartSearch;
const getSearchSuggestions = async (req, res) => {
    try {
        const query = (req.query.q || req.query.query || '');
        const result = await ai_search_service_1.AISearchService.getSearchSuggestions(query);
        return (0, response_utils_1.sendSuccess)(res, 'Search suggestions retrieved', result);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to get search suggestions', error.message, 500);
    }
};
exports.getSearchSuggestions = getSearchSuggestions;
// ==========================================
// 3. ADMIN GENERATIVE AI SUITE
// ==========================================
const generateDescription = async (req, res) => {
    try {
        const result = await ai_generator_service_1.AIGeneratorService.generateProductDescription(req.body);
        return (0, response_utils_1.sendSuccess)(res, 'AI Product Description generated successfully', result);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to generate product description', error.message, 500);
    }
};
exports.generateDescription = generateDescription;
const generateSeo = async (req, res) => {
    try {
        const result = await ai_generator_service_1.AIGeneratorService.generateSeoMetadata(req.body);
        return (0, response_utils_1.sendSuccess)(res, 'AI SEO Metadata generated successfully', result);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to generate SEO metadata', error.message, 500);
    }
};
exports.generateSeo = generateSeo;
const generateKeywords = async (req, res) => {
    try {
        const result = await ai_generator_service_1.AIGeneratorService.generateKeywordsAndTags(req.body);
        return (0, response_utils_1.sendSuccess)(res, 'AI Keywords generated successfully', result);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to generate keywords', error.message, 500);
    }
};
exports.generateKeywords = generateKeywords;
const generateFaqs = async (req, res) => {
    try {
        const result = await ai_generator_service_1.AIGeneratorService.generateFaqs(req.body);
        return (0, response_utils_1.sendSuccess)(res, 'AI FAQs generated successfully', result);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to generate FAQs', error.message, 500);
    }
};
exports.generateFaqs = generateFaqs;
const generateAutoTags = async (req, res) => {
    try {
        const result = await ai_generator_service_1.AIGeneratorService.generateAutoTags(req.body);
        return (0, response_utils_1.sendSuccess)(res, 'AI Auto Tags generated successfully', result);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to generate tags', error.message, 500);
    }
};
exports.generateAutoTags = generateAutoTags;
// ==========================================
// 4. IMAGE OPTIMIZATION
// ==========================================
const optimizeImage = async (req, res) => {
    try {
        const { imageUrl, width, quality } = req.body;
        if (!imageUrl)
            return (0, response_utils_1.sendError)(res, 'imageUrl is required', null, 400);
        const result = image_optimizer_service_1.ImageOptimizerService.optimizeImageUrl(imageUrl, { width, quality });
        return (0, response_utils_1.sendSuccess)(res, 'Image optimized successfully', result);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to optimize image', error.message, 500);
    }
};
exports.optimizeImage = optimizeImage;
// ==========================================
// 5. BUSINESS ANALYTICS & DEMAND FORECAST
// ==========================================
const getBusinessAnalytics = async (_req, res) => {
    try {
        const result = await ai_analytics_service_1.AIAnalyticsService.getBusinessAnalytics();
        return (0, response_utils_1.sendSuccess)(res, 'AI Business Analytics forecast retrieved', result);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to get analytics forecast', error.message, 500);
    }
};
exports.getBusinessAnalytics = getBusinessAnalytics;
// ==========================================
// 6. CUSTOMER CHAT ASSISTANT (RAG)
// ==========================================
const chatWithAssistant = async (req, res) => {
    try {
        const { messages } = req.body;
        const result = await ai_chat_service_1.AIChatService.chat(messages || []);
        return (0, response_utils_1.sendSuccess)(res, 'Assistant response generated', result);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to process chat message', error.message, 500);
    }
};
exports.chatWithAssistant = chatWithAssistant;
//# sourceMappingURL=ai.controller.js.map