import { describe, expect, it } from 'vitest';
import type { TenantEvent } from '../../api';
import { buildEventStatusCounts, filterEventsByStatus, resolveEventStatusKey } from './eventFilters';

const baseEvent = (overrides: Partial<TenantEvent>): TenantEvent => ({
  id: overrides.id ?? 1,
  slug: overrides.slug ?? 'event',
  name: overrides.name ?? 'Event',
  event_date: overrides.event_date ?? null,
  status: overrides.status ?? 'published',
  settings: overrides.settings ?? {},
  tasks_count: overrides.tasks_count ?? 0,
  active_invites_count: overrides.active_invites_count ?? 0,
  total_invites_count: overrides.total_invites_count ?? 0,
  photo_count: overrides.photo_count ?? 0,
  likes_sum: overrides.likes_sum ?? 0,
  engagement_mode: overrides.engagement_mode ?? 'tasks',
  event_type_id: overrides.event_type_id ?? null,
  event_type: overrides.event_type ?? null,
});

describe('resolveEventStatusKey', () => {
  it('returns draft when event is draft', () => {
    const event = baseEvent({ status: 'draft' });
    expect(resolveEventStatusKey(event)).toBe('draft');
  });

  it('returns past when published event date is in the past', () => {
    const event = baseEvent({ status: 'published', event_date: new Date(Date.now() - 86400_000).toISOString() });
    expect(resolveEventStatusKey(event)).toBe('past');
  });

  it('returns upcoming when published event date is in the future', () => {
    const event = baseEvent({ status: 'published', event_date: new Date(Date.now() + 86400_000).toISOString() });
    expect(resolveEventStatusKey(event)).toBe('upcoming');
  });
});

describe('buildEventStatusCounts', () => {
  it('counts events by status', () => {
    const events: TenantEvent[] = [
      baseEvent({ id: 1, status: 'draft' }),
      baseEvent({ id: 2, status: 'published', event_date: new Date(Date.now() + 86400_000).toISOString() }),
      baseEvent({ id: 3, status: 'published', event_date: new Date(Date.now() - 86400_000).toISOString() }),
    ];
    const counts = buildEventStatusCounts(events);
    expect(counts.all).toBe(3);
    expect(counts.draft).toBe(1);
    expect(counts.upcoming).toBe(1);
    expect(counts.past).toBe(1);
  });
});

describe('filterEventsByStatus', () => {
  it('filters by status', () => {
    const events: TenantEvent[] = [
      baseEvent({ id: 1, status: 'draft' }),
      baseEvent({ id: 2, status: 'published', event_date: new Date(Date.now() - 86400_000).toISOString() }),
    ];
    const filtered = filterEventsByStatus(events, 'draft');
    expect(filtered).toHaveLength(1);
    expect(filtered[0]?.id).toBe(1);
  });
});
