// src/modules/manager/manager.processor.ts
import {OnWorkerEvent, Processor, WorkerHost} from '@nestjs/bullmq';
import {Job} from 'bullmq';
import {AIService} from '../../shared/ai.service';
import {InjectBot} from "nestjs-telegraf";
import {Context, Telegraf} from "telegraf";
import {_JsonToStr} from "../../shared/helper";
import {ContentWriterService} from "../content-writer/content-writer.service";
import {GenerateContentDto} from "../content-writer/dto/generate-content.dto";

@Processor('manager_task_queue')
export class ManagerProcessor extends WorkerHost {
    private adminChatId = process.env.TELEGRAM_ADMIN_ID || 0;

    constructor(
        private aiService: AIService,
        private contentWriterService: ContentWriterService,
        @InjectBot() private readonly bot: Telegraf<Context>,
    ) {
        super();
    }

    async process(job: Job<any, any, string>): Promise<any> {
        console.log(`ManagerProcessor ==> process => ${job.name}`);
        console.log('job_data', job.data);

        let {rawData, style, telegramChatId} = job.data;

        switch (job.name) {
            case 'analyze_news':
                // Gọi AI phân tích
                let analysis = await this.aiService.analyzeTrend(rawData, style);
                // console.log(analysis.result);

                // @ts-ignore
                await this.bot.telegram.sendMessage(this.adminChatId, _JsonToStr(analysis.result));

                if (analysis.is_relevant && analysis.hot_score > 7) {
                    // Đẩy tiếp sang Queue của AI-B (Facebook) và AI-C (TikTok)
                    return analysis;
                }
                return {status: 'skipped'};
            case 'asking': {
                // Gọi AI phân tích
                const returnQuestion = await this.aiService.askQuestion(rawData);
                // console.log(analysis.result);
                console.log('telegramChatId:', telegramChatId);
                await this.bot.telegram.sendMessage(telegramChatId > 0 ? telegramChatId : this.adminChatId,
                    returnQuestion.content,
                    {
                        parse_mode: 'Markdown',
                    }
                );

                return {status: 'completed'};
            }
            case 'enrich_x_context': {
                const dto: GenerateContentDto = {
                    topic: rawData,
                    enableReview: true,
                    language: 'vi',
                    useXEnrichment: true,
                }
                const xContent = await this.contentWriterService.generate(dto)
                console.log('enrich_x_context:', xContent);
                await this.bot.telegram.sendMessage(telegramChatId > 0 ? telegramChatId : this.adminChatId,
                    xContent.final,
                    {
                        parse_mode: 'Markdown',
                    }
                );

                return {status: 'completed'};
            }
        }
    }

    @OnWorkerEvent('completed')
    async onCompleted(job: Job<any>) {
        console.log(`ManagerProcessor ==> onCompleted => ${job.name}`);
        //console.log({job});

        return {status: 'completed'};

    }
}
