import { Injectable, BadRequestException } from '@nestjs/common';
import { OssService } from '../oss/oss.service';

@Injectable()
export class UploadService {
  constructor(private ossService: OssService) {
    if (!this.ossService.isEnabled()) {
      console.warn('警告: COS 未配置，文件上传功能可能无法正常使用');
    }
  }

  async uploadFile(
    file: Express.Multer.File,
    tenantId?: number,
    userId?: number,
  ): Promise<{ url: string; fileName: string; size: number }> {
    if (!file) {
      throw new BadRequestException('文件不存在');
    }

    if (!this.ossService.isEnabled()) {
      throw new BadRequestException('文件存储服务未配置，请联系管理员');
    }

    const result = await this.ossService.uploadFile(
      file.buffer,
      file.originalname,
      tenantId,
      userId,
    );

    return {
      url: result.url,
      fileName: result.fileName,
      size: file.size,
    };
  }
}
