// src/modules/manager/manager.service.ts
import {Injectable} from '@nestjs/common';
import {InjectQueue} from '@nestjs/bullmq';
import {Queue} from 'bullmq';
import {QuoteType} from "../content-writer/enum/quote-type.enum";

@Injectable()
export class ManagerService {
    constructor(
        @InjectQueue('content_writer_queue') private fbQueue: Queue,
        @InjectQueue('comment_writer_queue') private commentQueue: Queue,
        @InjectQueue('download_video_queue') private downloadVideoQueue: Queue,
        @InjectQueue('content_writer_completed_queue') private fbContentCompletedQueue: Queue,
        @InjectQueue('manager_task_queue') private managerTaskQueue: Queue,
    ) {
    }

    async handleAnalyzeNewsTrend(question: string) {
        await this.managerTaskQueue.add('analyze_news', {
            rawData: question,
            style: 'analytic'
        }, {attempts: 1, backoff: 5000});
        // // 2. Đẩy việc cho AI-C tìm video TikTok
        // await this.ttQueue.add('find_video', {
        //     keyword: trendData.keyword,
        // }, {attempts: 2});
    }

    async handleDownloadVideo(
        url: string,
        telegramChatId: number = 0
    ) {
        await this.downloadVideoQueue.add('download', {
            url: url,
            type: 'video/mp4',
            telegramChatId
        }, {attempts: 1, backoff: 5000})
    }

    async handleDownloadVideoMulti(
        urls: Array<string>,
        telegramChatId: number = 0
    ) {
        await this.downloadVideoQueue.add('download_multi', {
            urls: urls,
            telegramChatId
        }, {attempts: 1, backoff: 5000})
    }

    async handleDownloadTiktok(url: string, telegramChatId: number = 0) {
        await this.downloadVideoQueue.add('download_tiktok', {
            url: url,
            type: 'video/mp4',
            telegramChatId
        }, {attempts: 1, backoff: 5000})
    }

    async handleDownloadFacebookReels(url: string, telegramChatId: number = 0) {
        await this.downloadVideoQueue.add('facebook_reels', {
            url: url,
            type: 'video/mp4',
            telegramChatId,
            sourceType: 'fb'
        }, {attempts: 1, backoff: 5000})
    }

    async handleDownloadMp4Url(
        url: string,
        sourceType = 'x',
        telegramChatId: number = 0
    ) {
        await this.downloadVideoQueue.add('mp4url', {
            url: url,
            type: 'video/mp4',
            telegramChatId,
            sourceType
        }, {attempts: 1, backoff: 5000})
    }

    async handleAskQues(question: string, telegramChatId: number = 0) {
        await this.managerTaskQueue.add('asking', {
            rawData: question,
            telegramChatId
        }, {attempts: 1, backoff: 5000});
        // // 2. Đẩy việc cho AI-C tìm video TikTok
        // await this.ttQueue.add('find_video', {
        //     keyword: trendData.keyword,
        // }, {attempts: 2});
    }

    async handleAskXContext(question: string, telegramChatId: number = 0) {
        await this.managerTaskQueue.add('enrich_x_context', {
            rawData: question,
            telegramChatId
        }, {attempts: 1, backoff: 5000});
        // // 2. Đẩy việc cho AI-C tìm video TikTok
        // await this.ttQueue.add('find_video', {
        //     keyword: trendData.keyword,
        // }, {attempts: 2});
    }

    async manualTrigger(keyword: string, style = 'general', preferLanguage = 'en') {
        // 1. Đẩy việc cho AI-B viết bài FB
        const manualtriggerresult = await this.fbQueue.add('generate_post_ver1', {
                title: keyword,
                style,
                language: preferLanguage,
            }, {
                attempts: 1,
                backoff: 5000,
                removeOnComplete: true,
                removeOnFail: true,
            },
        );

        //console.log(manualtriggerresult);

        return {
            needsConfirm: 1,
            summary: '',
            id: '',
        }
    }

    async manualTriggerWriteWithReview(
        dto:
        {
            keyword: string,
            style: 'general',
            language: 'en',
            tone: undefined,
            postLength: undefined,
            telegramChatId: undefined,
        },
    ) {
        // 1. Đẩy việc cho AI-B viết bài FB
        const manualtriggerresult = await this.fbQueue.add('generate_post_ver2', {
                title: dto.keyword,
                style: dto.style,
                language: dto.language,
                tone: dto.tone,
                postLength: dto.postLength,
                enableReview: true,
                telegramChatId: dto.telegramChatId,
            }, {
                attempts: 1,
                backoff: 5000,
                removeOnComplete: true,
                removeOnFail: true,
            },
        );

        //console.log(manualtriggerresult);

        return {
            needsConfirm: 1,
            summary: '',
            id: '',
        }
    }

    async manualTriggerCommentLinkTwitter(TwitterUrl, preferLanguage = 'en', telegramChatId = '') {
        await this.commentQueue.add('generate_comment_twitter', {
                url: TwitterUrl,
                language: preferLanguage,
                telegramChatId,
            }, {
                attempts: 1,
                backoff: 5000,
                removeOnComplete: true,
                removeOnFail: true,
            },
        );

        //console.log(manualtriggerresult);

        return {
            needsConfirm: 1,
            summary: '',
            id: '',
        }
    }

    async manualTriggerCommentAsText({
                                         comtext,
                                         tone,
                                         agle,
                                         language = 'en',
                                         tweetId = undefined,
                                         tweetUrl = undefined,
                                         chatId = undefined,
                                     }) {
        await this.commentQueue.add('generate_comment_as_text_twitter', {
                comtext,
                language,
                tone,
                agle,
                tweetId,
                tweetUrl,
                telegramChatId: chatId
            }, {
                attempts: 1,
                backoff: 5000,
                removeOnComplete: true,
                removeOnFail: true,
            },
        );

        //console.log(manualtriggerresult);

        return {
            needsConfirm: 1,
            summary: '',
            id: '',
        }
    }

    async manualTriggerQuoteLinkTwitter(TwitterUrl, quoteType?: QuoteType, preferLanguage = 'en', telegramChatId = '') {
        await this.commentQueue.add('generate_quote_twitter', {
                url: TwitterUrl,
                quoteType,
                language: preferLanguage,
                telegramChatId,
            }, {
                attempts: 1,
                backoff: 5000,
                removeOnComplete: true,
                removeOnFail: true,
            },
        );


        return {
            needsConfirm: 1,
            summary: '',
            id: '',
        }
    }

    async manualTriggerQuoteAsTextInput(userTopic, quoteType?: QuoteType, preferLanguage = 'en', telegramChatId = '') {
        await this.commentQueue.add('generate_quote_twitter_as_text_input', {
                quoteText: userTopic,
                quoteType,
                language: preferLanguage,
                telegramChatId
            }, {
                attempts: 1,
                backoff: 5000,
                removeOnComplete: true,
                removeOnFail: true,
            },
        );


        return {
            needsConfirm: 1,
            summary: '',
            id: '',
        }
    }

    async sendEventFbContentCompletedQueue(pgPostId: number, content?: string): Promise<void> {
        // 1. Đẩy việc vao queue completed
        // await this.fbContentCompletedQueue.add('generate_post_completed', {
        //     pgPostId: pgPostId,
        //     content,
        // }, {attempts: 1, backoff: 5000});
    }
}
