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

import {
  filterClassroomSupportStrategies,
  getClassroomSupportDailyStrategy,
  toClassroomSupportAgeFilter,
  toClassroomSupportCategoryFilter,
  toClassroomSupportZoneFilter,
} 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('matches search terms against strategy titles and descriptions', () => {
    const strategies = [
      createStrategy({
        id: 'tokens',
        title: 'Token Economy System',
        description: 'Reward system using tokens earned for desired behaviors.',
      }),
      createStrategy({
        id: 'breaks',
        title: 'Sensory Break Station',
        description: 'Create a calm station for self-regulation.',
      }),
    ];

    expect(filterClassroomSupportStrategies(strategies, createFilters({ searchQuery: 'economy' }), new Set()).map((strategy) => strategy.id)).toEqual(['tokens']);
    expect(filterClassroomSupportStrategies(strategies, createFilters({ searchQuery: 'desired behavior' }), new Set()).map((strategy) => strategy.id)).toEqual(['tokens']);
    expect(filterClassroomSupportStrategies(strategies, createFilters({ searchQuery: 'sensory regulation' }), new Set()).map((strategy) => strategy.id)).toEqual(['breaks']);
  });

  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('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');
  });
});
