import { Module } from '@nestjs/common';
import { UploadService } from './upload.service';
import { UploadController } from './upload.controller';
import { MulterModule } from '@nestjs/platform-express';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { KnowledgeBaseModule } from '../knowledge-base/knowledge-base.module'; // Import KnowledgeBaseModule
import * as multer from 'multer';
import * as fs from 'fs';
import * as path from 'path';
import { UserModule } from '../user/user.module';

@Module({
  imports: [
    KnowledgeBaseModule, // Add to
    UserModule,
    MulterModule.registerAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => {
        const uploadPath = configService.get<string>(
          'UPLOAD_FILE_PATH',
          './uploads',
        ); // Get upload path from env varor use default './uploads' use

        // Ensure upload directory exists
        if (!fs.existsSync(uploadPath)) {
          fs.mkdirSync(uploadPath, { recursive: true });
        }

        // Get max file size from env var, default 100MB
        const maxFileSize = parseInt(
          configService.get<string>('MAX_FILE_SIZE', '104857600'), // 100MB in bytes
        );

        return {
          storage: multer.diskStorage({
            destination: (req: any, file, cb) => {
              const tenantId = req.user?.tenantId || 'default';
              const fullPath = path.join(uploadPath, tenantId);

              if (!fs.existsSync(fullPath)) {
                fs.mkdirSync(fullPath, { recursive: true });
              }
              cb(null, fullPath);
            },
            filename: (req, file, cb) => {
              // Fix Chinese filename garbling
              file.originalname = Buffer.from(
                file.originalname,
                'latin1',
              ).toString('utf8');
              const uniqueSuffix =
                Date.now() + '-' + Math.round(Math.random() * 1e9);
              cb(
                null,
                `${file.fieldname}-${uniqueSuffix}${path.extname(file.originalname)}`,
              );
            },
          }),
          limits: {
            fileSize: maxFileSize, // File size limit
          },
        };
      },
      inject: [ConfigService],
    }),
  ],
  controllers: [UploadController],
  providers: [UploadService],
})
export class UploadModule { }
