'use client';

import { Icons } from '@/components/icons';
import { cn } from '@/lib/utils';

const IMAGE_TYPES = new Set([
  'image/jpeg',
  'image/jpg',
  'image/png',
  'image/gif',
  'image/webp'
]);

export function isPreviewableImage(fileType?: string | null) {
  return fileType ? IMAGE_TYPES.has(fileType.toLowerCase()) : false;
}

export function formatCertificateFileType(fileType?: string | null) {
  if (!fileType) return null;

  const normalized = fileType.trim().toLowerCase();
  const segment = normalized.includes('/') ? normalized.split('/').pop() : normalized;

  return segment ? segment.toUpperCase() : null;
}

export function CertificatePlaceholder({
  fileType,
  empty = false,
  compact = false,
  className
}: {
  fileType?: string | null;
  empty?: boolean;
  compact?: boolean;
  className?: string;
}) {
  const fileTypeLabel = formatCertificateFileType(fileType);

  return (
    <div
      className={cn(
        'bg-muted/40 text-muted-foreground flex h-full w-full flex-col items-center justify-center rounded-xl border border-dashed text-center',
        compact ? 'gap-1 px-2 py-3' : 'gap-2 p-6',
        className
      )}
    >
      <Icons.award className={compact ? 'h-6 w-6' : 'h-10 w-10'} />
      <div className={compact ? 'text-[10px] leading-tight' : 'text-sm font-medium'}>
        {empty ? 'ไม่มีไฟล์แนบ' : 'เกียรติบัตร'}
      </div>
      {!empty && fileTypeLabel ? (
        <div className={compact ? 'text-[10px] uppercase' : 'text-xs uppercase'}>
          {fileTypeLabel}
        </div>
      ) : null}
    </div>
  );
}
