import { join } from 'path';

/**
 * 檔案路徑工具函數
 * 用於處理檔案上傳和路徑管理
 */

/**
 * 獲取檔案上傳的絕對路徑（用於實際儲存）
 */
export function getUploadAbsolutePath(projectId: string | number, fileName: string): string {
  return join(process.cwd(), 'uploads', 'projects', projectId.toString(), fileName);
}

/**
 * 獲取檔案上傳的相對路徑（用於資料庫儲存）
 */
export function getUploadRelativePath(projectId: string | number, fileName: string): string {
  return `uploads/projects/${projectId}/${fileName}`;
}

/**
 * 獲取檔案上傳目錄的絕對路徑
 */
export function getUploadDirPath(projectId: string | number): string {
  return join(process.cwd(), 'uploads', 'projects', projectId.toString());
}

/**
 * 從相對路徑獲取絕對路徑
 */
export function getAbsolutePathFromRelative(relativePath: string): string {
  return join(process.cwd(), relativePath);
}

/**
 * 生成唯一檔案名稱
 */
export function generateUniqueFileName(originalName: string): string {
  const fileExtension = originalName.split('.').pop();
  const timestamp = Date.now();
  const randomString = Math.random().toString(36).substr(2, 9);
  return `${timestamp}_${randomString}.${fileExtension}`;
}

/**
 * 驗證檔案類型
 */
export function isValidFileType(mimeType: string): boolean {
  const allowedTypes = [
    'application/vnd.ms-powerpoint',
    'application/vnd.openxmlformats-officedocument.presentationml.presentation',
    'video/mp4',
    'video/avi',
    'video/quicktime',
    'application/pdf'
  ];
  return allowedTypes.includes(mimeType);
}

/**
 * 驗證檔案大小
 */
export function isValidFileSize(size: number, maxSizeMB: number = 100): boolean {
  const maxSize = maxSizeMB * 1024 * 1024; // 轉換為位元組
  return size <= maxSize;
}
