import {Action, Command, Ctx, On, Start, Update} from 'nestjs-telegraf';
import {Context, Scenes} from 'telegraf';
import {HttpException, Injectable} from "@nestjs/common";
import {ManagerService} from "../manager/manager.service";
import {PublishPageService} from "../social-api/publish.page.service";
import {_toNum} from "../../shared/helper";
import {XReaderService} from "../x-reader/x-reader.service";
import {TrendsService} from "../trends/trends.service";
import {get, isEmpty} from 'lodash'
import {
    WIZARD_COMMENT2_SCENE_ID,
    WIZARD_COMMENT_SCENE_ID,
    WIZARD_QUOTE_SCENE_MAIN,
    WIZARD_WRITER_SCENE_ID
} from "./telegram.constants";
import {XImageUploadService} from "../x-uploader/x-image.upload.service";
import {TextUtil} from "../../common/utils/text.util";
import {XStrategy} from "../social-api/x-router.service";
import {XCacheService} from "../x-cache/x-cache.service";
import {SqsPostService} from "../sqs-module/sqs.post.service";

@Injectable()
@Update()
export class TelegramUpdates {

    constructor(
        private readonly managerService: ManagerService,
        private readonly publishPageService: PublishPageService,
        private readonly xReaderService: XReaderService,
        private readonly trendsService: TrendsService,
        private readonly xUpload: XImageUploadService,
        private readonly cacheService: XCacheService,
        private readonly sqsPostService: SqsPostService
    ) {
    }

    @Start()
    async onStart(ctx: Context) {

        await ctx.reply('Hệ thống Automation đã sẵn sàng! Gửi /help để xem lệnh.');
    }

    @Command('help')
    async onHelpCommand(ctx: Context) {
        await ctx.reply('Sau đây là các tính năng... ' +
            '\n' +
            '/writecheap_[en|ja|ko|vi] your_topic... (viết bài bằng AI deepseek)\n' +
            '/write_[vi|en|ja|ko] your_topic... (viết bài bằng AI chatgpt + deepseek)\n' +
            '/manual_publish_post postNum\n' +
            '/analyzenews ... (tim trend,...) \n' +
            '/trendstat \n' +
            '/ask ... (hỏi với ai deepseek,...) \n' +
            '/trend_stat ... (...) \n' +
            '/xreader ... (...) \n' +
            '/xreaderbrowser ... (...) \n' +
            '/collectrss ... (...) \n' +
            '/comment link X... (...) \n' +
            '/quote link X... (...) \n' +
            '');
    }

    @Command(['download', 'dl'])
    async onDownloadVideo(ctx) {
        // @ts-ignore
        const {command, payload} = ctx;

        if (!payload) {
            await ctx.reply(`vui lòng đưa topic, ví dụ: /${command} link tiktok/fb`);
            return;
        }
        // Regex tổng hợp các loại link
        const tiktokRegex = /https?:\/\/(?:www\.|vm\.|vt\.|m\.)?tiktok\.com\/[^\s]+/gi;
        const youtubeRegex = /https?:\/\/(?:www\.)?(?:youtube\.com\/(?:watch\?v=|shorts\/)|youtu\.be\/)[^\s]+/gi;
        const facebookRegex = /https?:\/\/(?:www\.|fb\.)?(?:facebook\.com\/reels\/|watch\/)[^\s]+/gi;
        const mp4Regex = /https?:\/\/[^\s^"]+\.mp4(?:\?[^\s^"]*)?/gi;

// Cách 1: Lấy tất cả vào một mảng
        const allRegex = new RegExp(`${tiktokRegex.source}|${youtubeRegex.source}|${facebookRegex.source}|${mp4Regex.source}`, 'gi');
        const allLinks = payload.match(allRegex) || [];
        console.log({allLinks});

        console.log("Tất cả link tìm thấy:", allLinks);

        if (allLinks.length === 0) {
            await ctx.reply(`Khong tim thay link download`);
            return;
        }
        if (allLinks.length === 1) {
            await this.managerService.handleDownloadVideo(allLinks[0], ctx.chat.id);
        } else {
            await this.managerService.handleDownloadVideoMulti(allLinks, ctx.chat.id);
        }
        await ctx.reply(`vui lòng chờ download ....`);
    }

    @Command('downloadmp4url')
    async onDownloadMp4Url(ctx) {
        // @ts-ignore
        const {command, payload} = ctx;

        if (!payload) {
            await ctx.reply(`vui lòng đưa topic, ví dụ: /${command} link mp4`);
            return;
        }
        await ctx.reply(`vui lòng chờ download....`);
        await this.managerService.handleDownloadMp4Url(payload, 'x', ctx.chat.id);
    }

    @Command('comment')
    async onWizardCommand(@Ctx() ctx: Scenes.SceneContext): Promise<void> {
        await ctx.scene.enter(WIZARD_COMMENT_SCENE_ID);
    }

    @Command([
        'comvi',
        'comen',
        'comja',
        'comko',
        'comcn',
    ])
    async onCommentAsTextCommand(@Ctx() ctx: Scenes.SceneContext): Promise<void> {

        let language = 'en';
        // @ts-ignore
        let {command, payload} = ctx;
        if (['comvi', 'comvn', 'com_vi'].includes(command)) {
            language = 'vi';
        }
        if (['comko', 'comkr', 'com_ko'].includes(command)) {
            language = 'ko';
        }
        if (['comja', 'comjp', 'com_ja'].includes(command)) {
            language = 'ja';
        }
        if (['comcn'].includes(command)) {
            language = 'cn';
        }

        if (!payload) {
            await ctx.reply(`vui lòng đưa topic, ví dụ: /${command} bitcoin approach $100000`);
            return;
        }
        const chatId = ctx.chat?.id;
        payload = payload.trim(); // Gets "my_payload"
        console.log({payload});
        let tweetId = undefined;
        let tweetUrl = undefined;
        let _linkX = payload.replace('twitter.com', 'x.com').split('?')[0];
        const match = _linkX.match(/status\/(\d+)/);
        if (match) {
            //nêu match => get content x
            const xpost = await this.xReaderService.readXPost(_linkX);
            console.log('==> content text:' + xpost.text);
            payload = xpost.text;
            tweetId = xpost.tweetId;
            tweetUrl = _linkX;
        } else {

        }
        await ctx.scene.enter(WIZARD_COMMENT2_SCENE_ID, {
            language,
            userTopic: payload,
            tweetId,
            tweetUrl,
            chatId
        });
    }

    @Command([
        'quote_vi',
        'quotevi',
        'quotevn',
        'quote_en',
        'quoteen',
        'quote_ja',
        'quoteja',
        'quote_jp',
        'quotejp',
        'quote_ko',
        'quoteko',
        'quotecn',
    ])
    async onWizardQuote(@Ctx() ctx: Scenes.SceneContext): Promise<void> {
        // @ts-ignore
        let {command, payload} = ctx;
        if (['quote_jp', 'quotejp', 'quoteja'].includes(command)) {
            command = 'quote_ja';
        }
        if (['quote_vn', 'quotevi', 'quotevn'].includes(command)) {
            command = 'quote_vi';
        }
        if (['quoteen'].includes(command)) {
            command = 'quote_en';
        }
        if (['quoteko'].includes(command)) {
            command = 'quote_ko';
        }
        if (['quotecn'].includes(command)) {
            command = 'quote_cn';
        }
        if (!payload) {
            await ctx.reply(`vui lòng đưa url, ví dụ: /${command} link_x_post ?`);
            return;
        }
        const preferLanguage = command.split('_')[1];
        await ctx.scene.enter(WIZARD_QUOTE_SCENE_MAIN, {
            language: preferLanguage,
            linkUrl: payload,
            quoteText: '',
            quoteAs: 'quotelink',
        });
    }

    @Command([
        'quoteentext',
        'quotevitext',
        'quotejatext',
        'quotekotext',
        'quotecntext',
    ])
    async onQuoteAsTextCommand(@Ctx() ctx: Scenes.SceneContext): Promise<void> {
        // @ts-ignore
        let {command, payload} = ctx;
        let language = 'en';
        if (['quotevitext'].includes(command)) {
            language = 'vi';
        }

        if (['quotejatext'].includes(command)) {
            language = 'ja';
        }
        if (['quotecntext'].includes(command)) {
            language = 'cn';
        }

        if (['quotekotext'].includes(command)) {
            language = 'ko';
        }

        if (!payload) {
            await ctx.reply(`ví dụ: /${command} nội dung quote ?`);
            return;
        }
        await ctx.scene.enter(WIZARD_QUOTE_SCENE_MAIN, {
            language: language,
            quoteText: payload,
            quoteAs: 'quotetext',
        });
    }

    @Command('collectrss')
    async onCollectRssCommand(ctx: Context) {
        const result = await this.trendsService.collectAndStore('rss');

        ctx.reply('Collection completed');
        await ctx.reply(JSON.stringify(result.stats));
    }

    @Command(['writecheap_vi', 'writecheap_en', 'writecheap_ja', 'writecheap_ko'])
    async onWriteCheapCommand(@Ctx() ctx: Context) {
        // @ts-ignore
        const {command, payload} = ctx;
        const preferLanguage = command.split('_')[1];

        if (!payload) {
            await ctx.reply(`vui lòng đưa topic, ví dụ: /${command} bitcoin approach $100000`);
            return;
        }
        await this.managerService.manualTrigger(payload, '', preferLanguage);

        await ctx.reply(`Đang tìm kiếm tin tức về: ${payload} với ngôn ngữ ${preferLanguage}...`);
    }

    @Command([
        'write_vn',
        'write_vi',
        'writevi',
        'write_en',
        'writeen',
        'write_jp',
        'write_ja',
        'writeja',
        'write_kr',
        'write_ko',
        'writeko',
        'writecn',
        'write_cn',
    ])
    async onWriteSmartWithAIReviewerCommand(@Ctx() ctx: Scenes.SceneContext) {
        // @ts-ignore
        let {command, payload} = ctx;
        if (['write_jp', 'writejp', 'writeja'].includes(command)) {
            command = 'write_ja';
        }
        if (['write_kr', 'writekr', 'writeko'].includes(command)) {
            command = 'write_ko';
        }
        if (['write_vn', 'writevn', 'writevi'].includes(command)) {
            command = 'write_vi';
        }
        if (['writeen'].includes(command)) {
            command = 'write_en';
        }
        if (['writecn'].includes(command)) {
            command = 'write_cn';
        }
        const preferLanguage = command.split('_')[1];

        if (!payload) {
            await ctx.reply(`vui lòng đưa topic, ví dụ: /${command} bitcoin approach $100000`);
            return;
        }
        const chatId = ctx.chat?.id;
        if (payload.length < 200) {
            //check has link X
            const detectSendLinkX = TextUtil.detectLinkX(payload);
            if (detectSendLinkX.hasLinkX) {
                const xpost = await this.xReaderService.readXPost(detectSendLinkX.url);
                payload = xpost.text;
            }
        }

        await ctx.scene.enter(WIZARD_WRITER_SCENE_ID, {
            language: preferLanguage,
            userTopic: payload,
            chatId,
        });
        // await this.managerService.manualTriggerWrite2(payload, '', preferLanguage);

        // await ctx.reply(`Đang tìm kiếm tin tức về: ${payload} với ngôn ngữ ${preferLanguage}...`);
    }

    @Command('manual_publish_post')
    async manual_publish_post(@Ctx() ctx: Context) {
        // @ts-ignore
        const {command, payload} = ctx;
        if (!payload) {
            await ctx.reply('thiếu thông tin post num');
            return;
        }
        const postNo = 1 * payload;
        await ctx.reply(`Đang đẩy bài ${payload} lên Facebook, Twitter`);

        await ctx.reply(
            `🤖 **Vui lòng chọn kênh**`,
            {
                parse_mode: 'Markdown',
                reply_markup: {
                    inline_keyboard: [
                        [
                            {text: "🐦 Post X", callback_data: `publish-post_twitter_${postNo}`},
                            {text: "🚀 Post FB", callback_data: `publish-post_facebook_${postNo}`},
                        ],
                        [
                            {text: "🐦 Post X via browser", callback_data: `publish-post_twitterbrowser_${postNo}`},
                            {text: "🚀 Post all", callback_data: `publish-post_all_${postNo}`},
                        ],
                        [{text: "🗑️ Hủy bài", callback_data: `delete-post_${postNo}`}]
                    ]
                }
            }
        );

        // await this.publishPageService.publishToFacebook(postNo).then(() => {
        //     ctx.reply(`Đăng bài lên FB số ${postNo} thành công`);
        // }).catch(err => {
        //     ctx.reply(`PublishToFacebook error ${err.message}`);
        // });
        // await this.publishPageService.publishTwitter(postNo).then(() => {
        //     ctx.reply(`Đăng bài lên X số ${postNo} thành công`);
        // }).catch(err => {
        //     ctx.reply(`PublishTwitter error ${err.message}`);
        // });

    }

    @Command('analyzenews')
    async onAnalyzeNews(ctx: Context) {
        // @ts-ignore
        const {command, payload} = ctx;

        if (!payload) {
            await ctx.reply(`vui lòng đưa topic, ví dụ: /${command} Hot news today in japan ?`);
            return;
        }

        await this.managerService.handleAnalyzeNewsTrend(payload);
        await ctx.reply(`vui lòng đưa chờ phân tích ....`);
    }

    @Command('ask')
    async onAsk(ctx: Context) {
        // @ts-ignore
        const {command, payload} = ctx;

        if (!payload) {
            await ctx.reply(`vui lòng đưa topic, ví dụ: /${command} Hot news today in japan ?`);
            return;
        }
        console.log(ctx.chat);
        await this.managerService.handleAskQues(payload, ctx.chat?.id);
        await ctx.reply(`vui lòng đưa chờ phân tích ....`);
    }

    @Command('askx')
    async onAskXContext(ctx: Context) {
        // @ts-ignore
        const {command, payload} = ctx;

        if (!payload) {
            await ctx.reply(`vui lòng đưa topic, ví dụ: /${command} Hot news today in japan ?`);
            return;
        }
        console.log(ctx.chat);
        await this.managerService.handleAskXContext(payload, ctx.chat?.id);
        await ctx.reply(`vui lòng đưa chờ phân tích ....`);
    }

    @Command(['xreader', 'xreader_browser'])
    async onXReader(ctx: Context) {
        // @ts-ignore
        let {command, payload} = ctx;
        if (!payload) {
            await ctx.reply(`vui lòng đưa url, ví dụ: /${command} link_x_post ?`);
            return;
        }
        await ctx.reply(`vui lòng chờ phân tích ....`);

        const content = await this.xReaderService.readXPost(
            payload,
            command === 'xreader_browser' ? 'browser' : 'any'
        ).catch((err) => {
            ctx.reply(err.message);
            return err;
        });
        let textMessage = ``;
        try {
            textMessage += `🤖 crawler by: ${content.via}  \n\n`;
            if (content) {
                textMessage += `====== CONTENT ===== \n\n`;
                textMessage += `text: ${content.text} \n----------------------- \n`;
                if (!isEmpty(content.images)) {
                    textMessage += `images:  ${content.images.join(' , ')} \n----------------------- \n`;
                }
                if (!isEmpty(content.videos)) {
                    textMessage += `videos:  ${content.videos.join(' , ')}  \n`;
                }

                if (!isEmpty(content.quoted)) {
                    textMessage += `====== QUOTE ===== \n`;
                    textMessage += `quoted text: ${get(content, 'quoted.text')} \n----------------------- \n`;
                    if (!isEmpty(content?.quoted?.images)) {
                        textMessage += `quoted images: ${get(content, 'quoted.images', []).join(' , ')} \n----------------------- \n`;
                    }
                    if (!isEmpty(content?.quoted?.videos)) {
                        textMessage += `quoted videos: ${get(content, 'quoted.videos', []).join(' , ')} \n----------------------- \n`;
                    }
                }
            }
            await ctx.reply(textMessage, {parse_mode: 'HTML'});
        } catch (err) {
            console.log(err);
            ctx.reply(err.message);
        }
    }

    @Action(/publish-post_(.+)/)
    async onPublishFB(ctx: Context) {
        const telegramChatId = ctx.chat?.id;
        const callbackData = (ctx.callbackQuery as any).data;
        const social = callbackData.split('_')[1];
        const postId = _toNum(callbackData.split('_')[2]);
        const xUsername = callbackData.split('_')[3];

        await ctx.answerCbQuery(`Đang đẩy bài lên Fb,X ${xUsername}...`);

        const promiseSettedFunc = [];
        let allowTw = 0;
        let allowFb = 0;
        let twStrageryPost = XStrategy.API_ONLY;
        let publishTo: any = [];
        switch (social) {
            case 'all':
                allowTw = 1;
                allowFb = 1;
                publishTo = ['x', 'fb'];
                break;
            case 'twitter':
                allowTw = 1;
                publishTo = ['x'];
                twStrageryPost=XStrategy.BROWSER_FIRST
                break;
            case 'twitterbrowser':
                allowTw = 1;
                publishTo = ['x'];
                twStrageryPost = XStrategy.BROWSER_ONLY;
                break;
            case 'facebook':
                allowFb = 1;
                publishTo = ['fb'];
                break;
        }

        try {
            // @ts-ignore
            const messageText = ctx.callbackQuery.message.text;

            await this.sqsPostService.sendMessage(xUsername,
                {
                    name: 'generate_post_completed',
                    type: 'X_POSTER_TWEET',
                    needConfirm: 1,
                    content: messageText,
                    autoPublish: 1,
                    telegramChatId,
                    xUsername,
                    publishTo,
                    xSubmitProvider: XStrategy.BROWSER_FIRST  //cứ 3post api, có 1 post browser
                }
            )
            // const r = await this.publishPageService.relyX(messageText, tweetId)
            //@ts-ignore
            await ctx.editMessageText(`✅ ** Đã gửi tweet sang queue ${xUsername} ID bài viết: ${postId}`);


            // await Promise.allSettled([
            //         allowFb && this.publishPageService.publishToFacebook(postId).then(() => {
            //             ctx.reply(`Đăng bài lên FB số ${postId} thành công`);
            //         }).catch(err => {
            //             ctx.reply(`PublishToFacebook error ${err.message}`);
            //         }),
            //         allowTw && this.publishPageService.publishTwitter(postId, twStrageryPost).then((resp) => {
            //             ctx.reply(`Đăng bài lên X số ${postId} thành công, ${resp?.url}`);
            //         }).catch(err => {
            //             ctx.reply(`PublishTwitter error ${err.message}`);
            //         }),
            //     ]
            // )

            // await ctx.editMessageText(`✅ **ĐÃ ĐĂNG THÀNH CÔNG!**\nID bài viết: ${postId}`);
        } catch (e) {
            await ctx.reply(`❌ Lỗi gửi bài: ${e.message}`);
        }
    }

    @Action(/delete-post_(.+)/)
    async onDelete(ctx: Context) {
        const callbackData = (ctx.callbackQuery as any).data;
        const postId = _toNum(callbackData.split('_')[1]);
        // console.log(ctx.match);
        console.log('delete_post_id=', postId);

        await this.publishPageService.rejectedPost(postId);
        await ctx.answerCbQuery('Đã hủy bài viết.');
        await ctx.editMessageText('🗑️ Bài viết đã được loại bỏ khỏi hàng đợi.');
    }

    // @ts-ignore
    @Command(['trendstat', 'trend_stat'])
    async onTrendStatCommand(ctx: Context) {
        await ctx.reply(`Đang query..`);

        const {topTrends, byCategory} = await this.trendsService.getStats();

        let message = ``;
        topTrends.forEach(trend => {
            message += `\n${trend.title} - ${trend.category} - ${trend.score}\n`;
        });
        await ctx.reply(message, {
            parse_mode: 'HTML'
        });

        message = '';
        console.log({byCategory});
        byCategory.forEach(trend => {
            message += `\n${trend.category} - ${trend._count._all} - ${trend._avg.score} \n`;
        });
        await ctx.reply(message, {
            parse_mode: 'HTML'
        });
        ctx.reply('Done')
    }

    @Action(/publish-reply-twitter1_(.+)/)
    async onActionSubmitReplyX1(ctx: Context) {
        const callbackData = (ctx.callbackQuery as any).data;
        // @ts-ignore
        const messageText = ctx.callbackQuery.message.text;

        const callbackDataArr = callbackData.split('_');
        const tweetId = callbackDataArr[1];
        const xUsername = callbackDataArr[2];

        await ctx.answerCbQuery('Đang đẩy reply lên Twitter...');

        console.log(messageText);
        console.log(`publish-reply-twitter1_ ${tweetId} ${xUsername}`);
        const telegramChatId = ctx.chat?.id;
        try {
            const tweetUrl = await this.cacheService.getCacheTweetUrlById(tweetId);
            if (isEmpty(tweetUrl)) {
                throw new HttpException(`Không tìm thấy tweet url từ Id : ${tweetId}`, 500);
            }
            await this.sqsPostService.sendMessage(xUsername,
                {
                    tweetId: tweetId,
                    tweetUrl,
                    // name: 'generate_comment_twitter_completed',
                    type: 'X_POSTER_REPLY',
                    needConfirm: 1,
                    content: messageText,
                    autoPublish: 1,
                    telegramChatId,
                    xUsername,
                    xSubmitProvider: XStrategy.BROWSER_ONLY  //cứ 3post api, có 1 post browser
                }
            )
            // const r = await this.publishPageService.relyX(messageText, tweetId)
            //@ts-ignore
            if (r.success) {
                await ctx.editMessageText(`✅ ** Reply success !**\n ID bài viết: ${tweetId}`);
                await ctx.reply(`✅ ** Reply success !`);
            } else {
                //@ts-ignore
                await ctx.reply(`❌ Lỗi reply: ${r.error} `);
            }
        } catch (err) {
            console.log(err);
            const detail = get(err, 'data.detail', '')
            await ctx.reply(`❌ Lỗi reply: ${err.message} `);
            await ctx.reply(`=> ${detail}`);
        }
    }

    @Action(/publish-reply-twitter_(.+)/)
    async onActionSubmitReplyX(ctx: Context) {
        const callbackData = (ctx.callbackQuery as any).data;
        // @ts-ignore
        const messageText = ctx.callbackQuery.message.text;

        const callbackDataArr = callbackData.split('_');
        const tweetId = callbackDataArr[1];
        const xUsername = callbackDataArr[2];

        await ctx.answerCbQuery('Đang đẩy reply lên Twitter...');
        console.log(`Đang đẩy reply lên Twitter 0...`);

        console.log(messageText);
        const telegramChatId = ctx.chat?.id;
        try {
            const tweetUrl = await this.cacheService.getCacheTweetUrlById(tweetId);
            if (isEmpty(tweetUrl)) {
                console.error(`Không tìm thấy tweet url từ Id : ${tweetId}`);
                throw new HttpException(`Không tìm thấy tweet url từ Id : ${tweetId}`, 500);
            }
            await this.sqsPostService.sendMessage(xUsername,
                {
                    tweetId: tweetId,
                    tweetUrl,
                    // name: 'generate_comment_twitter_completed',
                    type: 'X_POSTER_REPLY',
                    needConfirm: 1,
                    content: messageText,
                    autoPublish: 1,
                    telegramChatId,
                    xUsername,
                    xSubmitProvider: XStrategy.BROWSER_ONLY  //cứ 3post api, có 1 post browser
                }
            )
            // const r = await this.publishPageService.relyX(messageText, tweetId)
            //@ts-ignore
                await ctx.editMessageText(`✅ ** Đã gửi tin sang queue ${xUsername} ID bài viết: ${tweetId}`);
                // await ctx.reply(`✅ Đã gửi tin sang queue ${xUsername}!, ID bài viết: ${tweetId}`);
            // else {
            //     //@ts-ignore
            //     await ctx.reply(`❌ Lỗi reply: ${r.error} `);
            // }
        } catch (err) {
            console.log(err);
            const detail = get(err, 'data.detail', '')
            await ctx.reply(`❌ Lỗi reply: ${err.message} `);
            await ctx.reply(`=> ${detail}`);
        }
    }

    @Action(/publish-quote-twitter_(.+)/)
    async onActionSubmitQuoteX(ctx: Context) {
        const callbackData = (ctx.callbackQuery as any).data;
        // @ts-ignore
        const messageText = ctx.callbackQuery.message.text;

        // const tweetId = callbackData.split('_')[1];
        const callbackDataArr = callbackData.split('_');
        const tweetId = callbackDataArr[1];
        const xUsername = callbackDataArr[2];

        await ctx.answerCbQuery('Đang đẩy quote lên Twitter...');
        console.log(`Đang đẩy quote lên Twitter...`);

        console.log(messageText);
        console.log(tweetId);
        console.log({xUsername, tweetId});

        const telegramChatId = ctx.chat?.id;

        try {
            const tweetUrl = await this.cacheService.getCacheTweetUrlById(tweetId);
            if (isEmpty(tweetUrl)) {
                console.error(`Không tìm thấy tweet url từ Id : ${tweetId}`);

                throw new HttpException(`Không tìm thấy tweet url từ Id : ${tweetId}`, 500);
            }

            await this.sqsPostService.sendMessage(xUsername,
                {
                    tweetId: tweetId,
                    tweetUrl,
                    // name: 'generate_comment_twitter_completed',
                    type: 'X_POSTER_QUOTE',
                    needConfirm: 1,
                    content: messageText,
                    autoPublish: 1,
                    telegramChatId,
                    xUsername,
                    xSubmitProvider: XStrategy.BROWSER_ONLY  //cứ 3post api, có 1 post browser
                }
            )
            await ctx.reply(`✅ Gửi tin sang queue.`);
            //await this.publishPageService.quoteX(messageText, tweetId)
            //const r = await this.publishPageService.quoteX(messageText, tweetId)
            // @ts-ignore
            // if (r.success) {
            //     await ctx.editMessageText(`✅ ** Quote success !**`);
            //     await ctx.reply(`✅ ** Quote success !**`);
            // } else {
            //     await ctx.reply(`❌ Lỗi quote`);
            //     // @ts-ignore
            //     await ctx.reply(r.error);
            // }
        } catch (err) {
            console.log(err);
            const detail = get(err, 'data.detail', '')
            await ctx.reply(`❌ Lỗi quote: ${err.message} `);
            await ctx.reply(`=> ${detail}`);
        }


    }

    @Action([
        /delete-reply_(.+)/,
        /delete-quote_(.+)/
    ])
    async onDeleteReplyX(ctx: Context) {
        const callbackData = (ctx.callbackQuery as any).data;
        const postId = _toNum(callbackData.split('_')[1]);
        // console.log(ctx.match);
        console.log('delete_post_id=', postId);

        // await this.publishPageService.rejectedPost(postId);
        await ctx.answerCbQuery('Đã hủy bài viết.');
        await ctx.editMessageText('🗑️ Bài viết đã được loại bỏ khỏi hàng đợi.');
    }

    @On('photo')
    async handlePhoto(@Ctx() ctx: Context) {
        try {
            const photos = (ctx.message as any).photo;
            const largest = photos[photos.length - 1];
            const fileId = largest.file_id;

            // Lấy link để tải ảnh về (nếu cần)
            const fileUrl = await ctx.telegram.getFileLink(fileId);

            await ctx.reply(`Đã nhận được ảnh! Bạn có thể xem tại: ${fileUrl}`);

            // // 1. get file path
            // const res = await axios.get(
            //     `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/getFile`,
            //     {
            //         params: {file_id: fileId},
            //     }
            // );
            //
            // const filePath = res.data.result.file_path;
            //
            // // 2. build download url
            // const fileUrl = `https://api.telegram.org/file/bot${process.env.TELEGRAM_BOT_TOKEN}/${filePath}`;
            //
            // // 3. download buffer
            // const img = await axios.get(fileUrl, {
            //     responseType: 'arraybuffer',
            // });
            //
            // const buffer = Buffer.from(img.data);

            // 4. upload to X
            const mediaId = await this.xUpload.uploadImageFromUrl(fileUrl.toString());


            await ctx.reply(`✅ Uploaded mediaId: ${mediaId}`);
        } catch (e) {
            console.error(e);
            await ctx.reply('❌ Upload failed');
        }
    }

    @Command('chatid')
    async onGetChatId(ctx: Context) {
        // @ts-ignore
        ctx.reply(ctx.chat.id)
    }
}
