import { describe, it, expect, vi, beforeEach } from 'vitest'
import { BRAND } from '../../shared/constants/colors'

type VideoPlayerMode = 'preview' | 'background'

interface VideoPlayerConfig {
  loop: boolean
  muted: boolean
  volume: number
}

function getVideoPlayerConfig(mode: VideoPlayerMode): VideoPlayerConfig {
  return {
    loop: true,
    muted: mode === 'preview',
    volume: mode === 'background' ? 0.3 : 0,
  }
}

function shouldShowGradient(videoUrl: string | undefined): boolean {
  return !videoUrl
}

function shouldPlayVideo(isPlaying: boolean, videoUrl: string | undefined): boolean {
  return isPlaying && !!videoUrl
}

describe('VideoPlayer', () => {
  describe('video player configuration', () => {
    it('should configure preview mode with muted audio', () => {
      const config = getVideoPlayerConfig('preview')
      expect(config.loop).toBe(true)
      expect(config.muted).toBe(true)
      expect(config.volume).toBe(0)
    })

    it('should configure background mode with low volume', () => {
      const config = getVideoPlayerConfig('background')
      expect(config.loop).toBe(true)
      expect(config.muted).toBe(false)
      expect(config.volume).toBe(0.3)
    })

    it('should always loop regardless of mode', () => {
      expect(getVideoPlayerConfig('preview').loop).toBe(true)
      expect(getVideoPlayerConfig('background').loop).toBe(true)
    })
  })

  describe('gradient fallback', () => {
    it('should show gradient when no video URL', () => {
      expect(shouldShowGradient(undefined)).toBe(true)
      expect(shouldShowGradient('')).toBe(true)
    })

    it('should not show gradient when video URL exists', () => {
      expect(shouldShowGradient('https://example.com/video.m3u8')).toBe(false)
      expect(shouldShowGradient('https://example.com/video.mp4')).toBe(false)
    })
  })

  describe('playback control', () => {
    it('should play when isPlaying is true and video exists', () => {
      expect(shouldPlayVideo(true, 'https://example.com/video.mp4')).toBe(true)
    })

    it('should not play when isPlaying is false', () => {
      expect(shouldPlayVideo(false, 'https://example.com/video.mp4')).toBe(false)
    })

    it('should not play when no video URL', () => {
      expect(shouldPlayVideo(true, undefined)).toBe(false)
      expect(shouldPlayVideo(true, '')).toBe(false)
    })
  })

  describe('default gradient colors', () => {
    it('should use brand colors as default gradient', () => {
      const defaultColors = [BRAND.PRIMARY, BRAND.SECONDARY]
      expect(defaultColors[0]).toBe(BRAND.PRIMARY)
      expect(defaultColors[1]).toBe(BRAND.SECONDARY)
    })
  })

  describe('video URL validation', () => {
    it('should accept HLS streams', () => {
      const hlsUrl = 'https://example.com/video.m3u8'
      expect(shouldShowGradient(hlsUrl)).toBe(false)
    })

    it('should accept MP4 files', () => {
      const mp4Url = 'https://example.com/video.mp4'
      expect(shouldShowGradient(mp4Url)).toBe(false)
    })

    it('should handle null/undefined', () => {
      expect(shouldShowGradient(null as any)).toBe(true)
      expect(shouldShowGradient(undefined)).toBe(true)
    })
  })

  describe('mode-specific behavior', () => {
    it('preview mode should be silent', () => {
      const previewConfig = getVideoPlayerConfig('preview')
      expect(previewConfig.muted || previewConfig.volume === 0).toBe(true)
    })

    it('background mode should have audible audio', () => {
      const bgConfig = getVideoPlayerConfig('background')
      expect(bgConfig.volume).toBeGreaterThan(0)
    })
  })
})
