import { describe, expect, it } from 'vitest';
import {
  calculateCommunityStats,
  filterCommunityOrganizations,
  listCommunityCategories,
  toCommunityAgeFilter,
  toCommunityCategoryFilter,
  toCommunityPartnershipFilter,
} from '@/business/community/selectors';
import type { CommunityOrganization } from '@/shared/types/community';

const organizations: readonly CommunityOrganization[] = [
  {
    id: 'food-bank',
    name: 'Sunshine Food Bank',
    category: 'Food & Nutrition',
    description: 'Meal kit packing and family food support.',
    address: '100 Main St',
    phone: '(602) 555-0100',
    email: 'volunteer@example.org',
    website: 'example.org',
    distance: '2.0 mi',
    opportunities: ['Meal kits'],
    partnershipType: 'both',
    ageGroups: ['K-2', '3-5', '6-8'],
    rating: 5,
    featured: true,
  },
  {
    id: 'library',
    name: 'Phoenix Library',
    category: 'Education & Literacy',
    description: 'Reading buddies and shelf organization.',
    address: '200 Book Ave',
    phone: '(602) 555-0200',
    email: 'library@example.org',
    website: 'library.example.org',
    distance: '4.0 mi',
    opportunities: ['Reading buddies'],
    partnershipType: 'school-partnership',
    ageGroups: ['K-2', '3-5'],
    rating: 4,
    featured: false,
  },
  {
    id: 'cleanup',
    name: 'Neighborhood Cleanup Coalition',
    category: 'Environment & Nature',
    description: 'Park cleanups and recycling drives.',
    address: 'Various',
    phone: '(602) 555-0300',
    email: 'cleanup@example.org',
    website: 'cleanup.example.org',
    distance: '1.0 mi',
    opportunities: ['Park cleanup'],
    partnershipType: 'community-service',
    ageGroups: ['6-8'],
    rating: 4,
    featured: false,
  },
];

describe('community selectors', () => {
  it('lists unique categories in sorted order', () => {
    expect(listCommunityCategories(organizations)).toEqual([
      'Education & Literacy',
      'Environment & Nature',
      'Food & Nutrition',
    ]);
  });

  it('normalizes category, partnership, and age filters', () => {
    const categories = listCommunityCategories(organizations);

    expect(toCommunityCategoryFilter('Food & Nutrition', categories)).toBe('Food & Nutrition');
    expect(toCommunityCategoryFilter('Missing', categories)).toBe('all');
    expect(toCommunityPartnershipFilter('school-partnership')).toBe('school-partnership');
    expect(toCommunityPartnershipFilter('unexpected')).toBe('all');
    expect(toCommunityAgeFilter('3-5')).toBe('3-5');
    expect(toCommunityAgeFilter('9-12')).toBe('all');
  });

  it('filters organizations by search, category, partnership, and age', () => {
    expect(filterCommunityOrganizations(organizations, '  food ', 'all', 'all', 'all')).toEqual([organizations[0]]);
    expect(filterCommunityOrganizations(organizations, '', 'Education & Literacy', 'all', 'all')).toEqual([
      organizations[1],
    ]);
    expect(filterCommunityOrganizations(organizations, '', 'all', 'school-partnership', 'all')).toEqual([
      organizations[0],
      organizations[1],
    ]);
    expect(filterCommunityOrganizations(organizations, '', 'all', 'community-service', '6-8')).toEqual([
      organizations[0],
      organizations[2],
    ]);
  });

  it('calculates catalog-level stats with saved count', () => {
    expect(calculateCommunityStats(organizations, 3)).toEqual({
      organizations: organizations.length,
      serviceProjects: organizations.filter((organization) => (
        organization.partnershipType !== 'school-partnership'
      )).length,
      schoolPartners: organizations.filter((organization) => (
        organization.partnershipType !== 'community-service'
      )).length,
      saved: 3,
    });
  });
});
