import {
  AllowedMediaType,
  AUDIO_MIMETYPES,
  IMAGE_MIMETYPES,
  MEDIA_MAX_SIZE_BYTES,
  VIDEO_MIMETYPES,
} from './allowed-media';

export const DIRECT_UPLOAD_FOLDERS = [
  'posts/images',
  'posts/video',
  'posts/audio',
  'users/avatar',
  'users/cover',
  'chat/media',
  'marketplace/images',
  'support/images',
  'collaboration/audio',
] as const;

export type DirectUploadFolder = (typeof DIRECT_UPLOAD_FOLDERS)[number];

export const DIRECT_UPLOAD_MIME_TYPES = [
  ...IMAGE_MIMETYPES,
  ...AUDIO_MIMETYPES,
  ...VIDEO_MIMETYPES,
] as const;

export type DirectUploadMimeType = (typeof DIRECT_UPLOAD_MIME_TYPES)[number];

type DirectUploadMediaDefinition = {
  extension: string;
  mediaType: AllowedMediaType;
};

type DirectUploadFolderPolicy = Partial<Record<AllowedMediaType, number>>;

const MEDIA_BY_MIME_TYPE: Record<DirectUploadMimeType, DirectUploadMediaDefinition> = {
  'image/jpeg': { extension: '.jpg', mediaType: 'image' },
  'image/png': { extension: '.png', mediaType: 'image' },
  'image/webp': { extension: '.webp', mediaType: 'image' },
  'audio/mpeg': { extension: '.mp3', mediaType: 'audio' },
  'audio/mp3': { extension: '.mp3', mediaType: 'audio' },
  'audio/mp4': { extension: '.m4a', mediaType: 'audio' },
  'audio/x-m4a': { extension: '.m4a', mediaType: 'audio' },
  'audio/m4a': { extension: '.m4a', mediaType: 'audio' },
  'audio/wav': { extension: '.wav', mediaType: 'audio' },
  'audio/x-wav': { extension: '.wav', mediaType: 'audio' },
  'audio/wave': { extension: '.wav', mediaType: 'audio' },
  'audio/aac': { extension: '.aac', mediaType: 'audio' },
  'audio/ogg': { extension: '.ogg', mediaType: 'audio' },
  'audio/webm': { extension: '.webm', mediaType: 'audio' },
  'video/mp4': { extension: '.mp4', mediaType: 'video' },
  'video/quicktime': { extension: '.mov', mediaType: 'video' },
  'video/webm': { extension: '.webm', mediaType: 'video' },
};

const FOLDER_POLICIES: Record<DirectUploadFolder, DirectUploadFolderPolicy> = {
  'posts/images': { image: MEDIA_MAX_SIZE_BYTES.postsImage },
  'posts/video': { video: MEDIA_MAX_SIZE_BYTES.postsVideo },
  'posts/audio': { audio: MEDIA_MAX_SIZE_BYTES.postsAudio },
  'users/avatar': { image: MEDIA_MAX_SIZE_BYTES.userImage },
  'users/cover': { image: MEDIA_MAX_SIZE_BYTES.userImage },
  'chat/media': {
    image: MEDIA_MAX_SIZE_BYTES.postsImage,
    audio: MEDIA_MAX_SIZE_BYTES.postsAudio,
    video: MEDIA_MAX_SIZE_BYTES.postsVideo,
  },
  'marketplace/images': { image: MEDIA_MAX_SIZE_BYTES.marketplaceImage },
  'support/images': { image: MEDIA_MAX_SIZE_BYTES.supportImage },
  'collaboration/audio': { audio: MEDIA_MAX_SIZE_BYTES.collaborationAudio },
};

export type ResolvedDirectUploadPolicy = DirectUploadMediaDefinition & {
  maxSizeBytes: number;
};

export function resolveDirectUploadPolicy(
  folder: string,
  mimeType: string,
): ResolvedDirectUploadPolicy | null {
  if (!DIRECT_UPLOAD_FOLDERS.includes(folder as DirectUploadFolder)) {
    return null;
  }

  const media = MEDIA_BY_MIME_TYPE[mimeType as DirectUploadMimeType];
  if (!media) {
    return null;
  }

  const maxSizeBytes = FOLDER_POLICIES[folder as DirectUploadFolder][media.mediaType];
  return maxSizeBytes ? { ...media, maxSizeBytes } : null;
}
