import { describe, expect, it } from 'vitest';

import {
  filterClassroomSupportStrategies,
  getClassroomSupportDailyStrategy,
  toClassroomSupportAgeFilter,
  toClassroomSupportCategoryFilter,
  toClassroomSupportZoneFilter,
  toggleClassroomSupportFavorite,
} from '@/business/classroom-support/selectors';
import type { ClassroomSupportFilters } from '@/business/classroom-support/types';
import type { Strategy } from '@/shared/types/app';

function createStrategy(overrides: Partial<Strategy> = {}): Strategy {
  return {
    id: 'strategy-1',
    title: 'Visual Schedule',
    description: 'Use a visual schedule.',
    implementationTip: 'Start small.',
    category: 'visual-support',
    ageGroup: 'All',
    zone: 'green',
    image: 'https://example.com/image.jpg',
    ...overrides,
  };
}

function createFilters(overrides: Partial<ClassroomSupportFilters> = {}): ClassroomSupportFilters {
  return {
    searchQuery: '',
    categoryFilter: 'all',
    ageFilter: 'all',
    zoneFilter: 'all',
    showFavoritesOnly: false,
    ...overrides,
  };
}

describe('classroom support selectors', () => {
  it('filters strategies by search, category, age, zone, and favorites', () => {
    const strategies = [
      createStrategy({ id: 'visual', title: 'Visual Schedule' }),
      createStrategy({ id: 'sensory', title: 'Sensory Break', category: 'sensory', ageGroup: 'K-2', zone: 'yellow' }),
    ];

    expect(filterClassroomSupportStrategies(strategies, createFilters({ searchQuery: 'break' }), new Set()).map((strategy) => strategy.id)).toEqual(['sensory']);
    expect(filterClassroomSupportStrategies(strategies, createFilters({ categoryFilter: 'sensory' }), new Set()).map((strategy) => strategy.id)).toEqual(['sensory']);
    expect(filterClassroomSupportStrategies(strategies, createFilters({ ageFilter: 'K-2' }), new Set()).map((strategy) => strategy.id)).toEqual(['sensory']);
    expect(filterClassroomSupportStrategies(strategies, createFilters({ zoneFilter: 'yellow' }), new Set()).map((strategy) => strategy.id)).toEqual(['sensory']);
    expect(filterClassroomSupportStrategies(strategies, createFilters({ showFavoritesOnly: true }), new Set(['visual'])).map((strategy) => strategy.id)).toEqual(['visual']);
  });

  it('selects a deterministic daily strategy', () => {
    const strategies = [
      createStrategy({ id: 'first' }),
      createStrategy({ id: 'second' }),
    ];

    expect(getClassroomSupportDailyStrategy(strategies, new Date(0))?.id).toBe('first');
    expect(getClassroomSupportDailyStrategy(strategies, new Date(86_400_000))?.id).toBe('second');
    expect(getClassroomSupportDailyStrategy([], new Date(0))).toBeNull();
  });

  it('toggles favorite IDs immutably', () => {
    const current = new Set(['strategy-1']);

    expect(Array.from(toggleClassroomSupportFavorite(current, 'strategy-1'))).toEqual([]);
    expect(Array.from(toggleClassroomSupportFavorite(current, 'strategy-2'))).toEqual(['strategy-1', 'strategy-2']);
  });

  it('normalizes invalid filter values', () => {
    expect(toClassroomSupportCategoryFilter('sensory')).toBe('sensory');
    expect(toClassroomSupportCategoryFilter('bad')).toBe('all');
    expect(toClassroomSupportAgeFilter('3-5')).toBe('3-5');
    expect(toClassroomSupportAgeFilter('bad')).toBe('all');
    expect(toClassroomSupportZoneFilter('red')).toBe('red');
    expect(toClassroomSupportZoneFilter('bad')).toBe('all');
  });
});
