import { CreationGalleryImage } from '@/types/Creation';

const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000';

export const getFullSizeImageByKey = (key: string) => {
  return `${apiUrl}/imageRepository/getImage?key=${key}`;
};

export const getImageByIdAndName = (imgId: string | number, fileName: string) => {
  return `${apiUrl}/imageRepository/getImageByIdAndName?imgId=${imgId}&imgName=${fileName}`;
};

export const getLightboxImages = (images: CreationGalleryImage[]) => {
  const baseFullSizeImgUrl = `${apiUrl}/imageRepository/getImage?key=`;

  return images.map((image) => {
    return {
      src: baseFullSizeImgUrl + image.key,
      alt: '',
      width: image.width,
      height: image.height,
      jogtulajdonos: image.jogtulajdonos,
      kepforrasa: image.kepforrasa,
      kepkeszitoje: image.kepkeszitoje,
      kepalairas: image.kepalairas,
      id: image.id,
    };
  });
};

export type ImageMetaLabels = {
  source: string;
  photo: string;
  rightsHolder: string;
};

const DEFAULT_IMAGE_META_LABELS: ImageMetaLabels = {
  source: 'Kép forrása',
  photo: 'Fotó',
  rightsHolder: 'Jogtulajdonos',
};

export function buildImageMetaText(
  image: CreationGalleryImage,
  includeCaption: boolean = true,
  labels: ImageMetaLabels = DEFAULT_IMAGE_META_LABELS
): string | undefined {
  if (!image) return undefined;
  const parts: string[] = [];
  const caption = image.kepalairas?.trim();
  if (includeCaption && caption && caption.length > 0) parts.push(caption);
  const items: Array<{ label: string; value: unknown }> = [
    { label: labels.source, value: image.kepforrasa },
    { label: labels.photo, value: image.kepkeszitoje },
    { label: labels.rightsHolder, value: image.jogtulajdonos },
  ];
  items.forEach((i) => {
    const value = i.value != null ? String(i.value).trim() : '';
    if (value.length > 0) parts.push(`${i.label}: ${value}`);
  });
  return parts.length > 0 ? parts.join(', ') : undefined;
}
