import {Module} from '@nestjs/common';
import {ConfigModule} from '@nestjs/config';

import {BullModule} from '@nestjs/bullmq';
import {AppController} from './app.controller';
import {AppService} from './app.service';
import {ManagerModule} from "./modules/manager/manager.module";
import {TelegramModule} from "./modules/telegram/telegram.module";
import {SocialModule} from "./modules/social-api/social.module";
import {AIService} from "./shared/ai.service";
import {TelegrafModule} from "nestjs-telegraf";
import {BullBoardModule} from "@bull-board/nestjs";
import {ExpressAdapter} from "@bull-board/express";
import {PrismaService} from "../prisma/prisma.service";
import {PgPostService} from "./shared/pg.post.service";
import {PrismaModule} from "../prisma/prisma.module";
import {CacheModule} from "@nestjs/cache-manager";
import KeyvRedis from "@keyv/redis";
import {XReaderModule} from './modules/x-reader/x-reader.module';
import {CollectorModule} from "./modules/collector/collector.module";
import {TrendsModule} from "./modules/trends/trends.module";
import configuration from "./common/config/configuration";
import {SchedulerModule} from "./modules/scheduler/scheduler.module";
import {ContentWriterModule} from "./modules/content-writer/content-writer.module";
import {TeleGrammYModule} from "./modules/tele-grammY/tele-grammY.module";
import {session} from "telegraf";
import {Redis} from '@telegraf/session/redis';
import {XUploaderModule} from "./modules/x-uploader/x-uploader.module";
import {TiktokDownloadModule} from "./modules/tiktok-download/tiktok.download.module";
import {XCacheModule} from "./modules/x-cache/x-cache.module";
import {XCacheService} from "./modules/x-cache/x-cache.service";
import {SqsModule} from "./modules/sqs-module/sqs.module";
import {SqsPostService} from "./modules/sqs-module/sqs.post.service";

@Module({
    imports: [
        // DevtoolsModule.register({
        //     http: process.env.NODE_ENV !== 'production',
        // }),
        ConfigModule.forRoot(
            {
                isGlobal: true,
                load: [configuration],
            },
        ),
        CacheModule.registerAsync({
            isGlobal: true,
            useFactory: () => ({
                stores: [
                    new KeyvRedis(`redis://127.0.0.1:6379/1`)
                ],
            }),
        }),
        BullModule.forRoot({
            connection: {
                host: '127.0.0.1',
                port: 6379,
            },
        }),
        //register the bull-board module forRoot in your app.module
        BullBoardModule.forRoot({
            route: "/queues",
            adapter: ExpressAdapter
        }),
        TelegrafModule.forRoot({
            token: process.env.TELEGRAM_BOT_TOKEN!,
            middlewares: [
                session({
                    store: Redis({
                        url: 'redis://127.0.0.1:6379/3', // Dùng DB 2 để tách biệt với CacheModule
                    })
                }), // BẮT BUỘC: Phải có store này thì Wizard mới nhảy bước được
                (ctx, next) => {
                    const allowedIds = (process.env.TELEGRAM_ALLOW_CHAT_IDS || '').split(',');
                    if (allowedIds.includes(''+ctx.chat?.id)) {
                        return next();
                    }
                    return ctx.reply('Xin lỗi, bạn không có quyền sử dụng bot này.');
                },
            ],
        }),
        // TelegrafModule.forRootAsync({
        //     useFactory: () => {
        //         // NestJS CacheManager stores (Keyv) usually expose the client
        //
        //         return {
        //             token: process.env.TELEGRAM_BOT_TOKEN!,
        //             middlewares: [
        //                 session({
        //                     store: new KeyvRedis({
        //                         url: 'redis://127.0.0.1:6379/3', // Dùng DB 2 để tách biệt với CacheModule
        //                     })
        //                 }),
        //             ],
        //         };
        //     },
        //     // token: process.env.TELEGRAM_BOT_TOKEN!,
        //     // middlewares: [session()],
        // }),
        XCacheModule,
        PrismaModule,
        ManagerModule,
        SocialModule,
        ContentWriterModule,
        CollectorModule,
        TrendsModule,
        TelegramModule,
        TeleGrammYModule,
        XReaderModule,
        SchedulerModule,
        XUploaderModule,
        TiktokDownloadModule,
        SqsModule,
    ],
    controllers: [AppController],
    providers: [AppService, AIService, PrismaService, PgPostService, SqsPostService],
    exports: [],
})
export class AppModule {
}
