import { AuthModule, JWT_CONSTANTS } from '@backend/auth'
import { validate } from '@backend/config'
import { LoggingModule } from '@backend/logging'
import KeyvValkey from '@keyv/valkey'
import { CacheModule } from '@nestjs/cache-manager'
import { Module } from '@nestjs/common'
import { ConfigModule } from '@nestjs/config'
import { EventEmitterModule } from '@nestjs/event-emitter'
import { JwtModule } from '@nestjs/jwt'
import { ScheduleModule } from '@nestjs/schedule'
import { CronModule } from '../base'
import { GOOGLE, REDIS_OPTIONS } from '../config'
import { GlobalModule } from '../global'
import { AwsModule } from './aws'
import { DatabaseModule } from './database'
import { QueueModule } from './queue'

@Module({
	imports: [
		LoggingModule,
		ConfigModule.forRoot({ validate }),
		DatabaseModule,
		AwsModule,
		QueueModule,
		CacheModule.registerAsync({
			isGlobal: true,
			useFactory: async () => ({
				stores: [new KeyvValkey({ host: REDIS_OPTIONS.HOST, port: REDIS_OPTIONS.PORT, db: REDIS_OPTIONS.DB })],
			}),
		}),
		ScheduleModule.forRoot(),
		EventEmitterModule.forRoot(),
		GlobalModule,
		CronModule,
		JwtModule.register({
			global: true,
			secret: JWT_CONSTANTS.SECRET,
			// TODO! Lower this value after implementing refresh tokens
			signOptions: { expiresIn: '24h' },
		}),
		AuthModule.forRoot({
			googleBookingServer: {
				username: GOOGLE.ACTION_CENTER.BOOKING_SERVER.USERNAME,
				password: GOOGLE.ACTION_CENTER.BOOKING_SERVER.PASSWORD,
			},
		}),
	],
})
export class CoreModule {}
