import { describe, it, expect } from 'vitest'
import {
  FREE_WORKOUT_IDS,
  FREE_WORKOUT_COUNT,
  isFreeWorkout,
  canAccessWorkout,
} from '../../shared/services/access'

describe('access service', () => {
  describe('FREE_WORKOUT_IDS', () => {
    it('should contain exactly 3 free workout IDs', () => {
      expect(FREE_WORKOUT_IDS).toHaveLength(3)
    })

    it('should include workout 1 (Full Body Ignite)', () => {
      expect(FREE_WORKOUT_IDS).toContain('1')
    })

    it('should include workout 11 (Core Crusher)', () => {
      expect(FREE_WORKOUT_IDS).toContain('11')
    })

    it('should include workout 43 (Dance Cardio)', () => {
      expect(FREE_WORKOUT_IDS).toContain('43')
    })

    it('should be readonly (immutable)', () => {
      // TypeScript enforces readonly at compile time; at runtime we verify the array content is stable
      const snapshot = [...FREE_WORKOUT_IDS]
      expect(FREE_WORKOUT_IDS).toEqual(snapshot)
    })
  })

  describe('FREE_WORKOUT_COUNT', () => {
    it('should equal the length of FREE_WORKOUT_IDS', () => {
      expect(FREE_WORKOUT_COUNT).toBe(FREE_WORKOUT_IDS.length)
    })

    it('should be 3', () => {
      expect(FREE_WORKOUT_COUNT).toBe(3)
    })
  })

  describe('isFreeWorkout', () => {
    it('should return true for free workout ID "1"', () => {
      expect(isFreeWorkout('1')).toBe(true)
    })

    it('should return true for free workout ID "11"', () => {
      expect(isFreeWorkout('11')).toBe(true)
    })

    it('should return true for free workout ID "43"', () => {
      expect(isFreeWorkout('43')).toBe(true)
    })

    it('should return false for non-free workout ID "2"', () => {
      expect(isFreeWorkout('2')).toBe(false)
    })

    it('should return false for non-free workout ID "10"', () => {
      expect(isFreeWorkout('10')).toBe(false)
    })

    it('should return false for non-free workout ID "44"', () => {
      expect(isFreeWorkout('44')).toBe(false)
    })

    it('should return false for empty string', () => {
      expect(isFreeWorkout('')).toBe(false)
    })

    it('should return false for non-existent ID', () => {
      expect(isFreeWorkout('999')).toBe(false)
    })
  })

  describe('canAccessWorkout', () => {
    describe('premium user', () => {
      it('should access free workout', () => {
        expect(canAccessWorkout('1', true)).toBe(true)
      })

      it('should access non-free workout', () => {
        expect(canAccessWorkout('2', true)).toBe(true)
      })

      it('should access any workout ID', () => {
        expect(canAccessWorkout('999', true)).toBe(true)
      })
    })

    describe('free user', () => {
      it('should access free workout ID "1"', () => {
        expect(canAccessWorkout('1', false)).toBe(true)
      })

      it('should access free workout ID "11"', () => {
        expect(canAccessWorkout('11', false)).toBe(true)
      })

      it('should access free workout ID "43"', () => {
        expect(canAccessWorkout('43', false)).toBe(true)
      })

      it('should NOT access non-free workout ID "2"', () => {
        expect(canAccessWorkout('2', false)).toBe(false)
      })

      it('should NOT access non-free workout ID "10"', () => {
        expect(canAccessWorkout('10', false)).toBe(false)
      })

      it('should NOT access non-free workout ID "50"', () => {
        expect(canAccessWorkout('50', false)).toBe(false)
      })

      it('should NOT access empty string ID', () => {
        expect(canAccessWorkout('', false)).toBe(false)
      })
    })
  })
})
