import { describe, expect, it } from 'vitest';
import type { TenantPhoto } from '../../api';
import { resolveLightboxSources } from './lightboxImage';

const basePhoto = (overrides: Partial<TenantPhoto>): TenantPhoto => ({
  id: overrides.id ?? 1,
  filename: overrides.filename ?? null,
  original_name: overrides.original_name ?? null,
  mime_type: overrides.mime_type ?? null,
  size: overrides.size ?? 1024,
  url: overrides.url ?? null,
  thumbnail_url: overrides.thumbnail_url ?? null,
  status: overrides.status ?? 'approved',
  is_featured: overrides.is_featured ?? false,
  likes_count: overrides.likes_count ?? 0,
  uploaded_at: overrides.uploaded_at ?? new Date().toISOString(),
  uploader_name: overrides.uploader_name ?? null,
  ingest_source: overrides.ingest_source ?? null,
  caption: overrides.caption ?? null,
});

describe('resolveLightboxSources', () => {
  it('prefers thumbnail for initial source and keeps full when different', () => {
    const photo = basePhoto({ thumbnail_url: '/thumb.jpg', url: '/full.jpg' });
    const sources = resolveLightboxSources(photo);
    expect(sources.initial).toBe('/thumb.jpg');
    expect(sources.full).toBe('/full.jpg');
  });

  it('falls back to full when thumbnail is missing', () => {
    const photo = basePhoto({ thumbnail_url: null, url: '/full.jpg' });
    const sources = resolveLightboxSources(photo);
    expect(sources.initial).toBe('/full.jpg');
    expect(sources.full).toBe('/full.jpg');
  });

  it('returns nulls when no sources exist', () => {
    const photo = basePhoto({ thumbnail_url: null, url: null });
    const sources = resolveLightboxSources(photo);
    expect(sources.initial).toBeNull();
    expect(sources.full).toBeNull();
  });
});
