import { describe, expect, it } from 'vitest';
import {
  buildFramePhotos,
  resolveIntervalMs,
  resolveItemsPerFrame,
  resolvePlaybackQueue,
} from '../useLiveShowPlayback';
import type { LiveShowPhoto, LiveShowSettings } from '../../services/liveShowApi';

const baseSettings: LiveShowSettings = {
  retention_window_hours: 12,
  moderation_mode: 'manual',
  playback_mode: 'newest_first',
  pace_mode: 'auto',
  fixed_interval_seconds: 8,
  layout_mode: 'single',
  effect_preset: 'film_cut',
  effect_intensity: 70,
  background_mode: 'blur_last',
};

const photos: LiveShowPhoto[] = [
  {
    id: 1,
    full_url: '/one.jpg',
    thumb_url: '/one-thumb.jpg',
    approved_at: '2025-01-01T10:00:00Z',
    is_featured: false,
    live_priority: 0,
  },
  {
    id: 2,
    full_url: '/two.jpg',
    thumb_url: '/two-thumb.jpg',
    approved_at: '2025-01-01T12:00:00Z',
    is_featured: true,
    live_priority: 2,
  },
  {
    id: 3,
    full_url: '/three.jpg',
    thumb_url: '/three-thumb.jpg',
    approved_at: '2025-01-01T11:00:00Z',
    is_featured: false,
    live_priority: 0,
  },
];

describe('useLiveShowPlayback helpers', () => {
  it('resolves items per frame per layout', () => {
    expect(resolveItemsPerFrame('single')).toBe(1);
    expect(resolveItemsPerFrame('split')).toBe(2);
    expect(resolveItemsPerFrame('grid_burst')).toBe(4);
  });

  it('builds a curated queue when configured', () => {
    const queue = resolvePlaybackQueue(photos, {
      ...baseSettings,
      playback_mode: 'curated',
    });

    expect(queue[0].id).toBe(2);
    expect(queue.every((photo) => photo.id === 2 || photo.live_priority > 0 || photo.is_featured)).toBe(true);
  });

  it('builds frame photos without duplicates when list is smaller', () => {
    const frame = buildFramePhotos([photos[0]], 0, 4);
    expect(frame).toHaveLength(1);
    expect(frame[0].id).toBe(1);
  });

  it('uses fixed interval when configured', () => {
    const interval = resolveIntervalMs(
      {
        ...baseSettings,
        pace_mode: 'fixed',
        fixed_interval_seconds: 12,
      },
      photos.length
    );

    expect(interval).toBe(12_000);
  });
});
