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

import {
  getDashboardDayOfYear,
  getDashboardGreeting,
  selectDashboardActiveZone,
  selectDashboardQuickActions,
  selectDashboardQuote,
  selectDashboardUpcomingEvents,
  toDashboardZoneColor,
} from '@/business/dashboard/selectors';
import type { CommunicationEventDto } from '@/shared/types/communications';
import type { DashboardEncouragingQuote } from '@/shared/types/dashboard';

const quotes: readonly DashboardEncouragingQuote[] = [
  { quote: 'First', author: 'A' },
  { quote: 'Second', author: 'B' },
];

function createEvent(id: string): CommunicationEventDto {
  return {
    id,
    title: `Event ${id}`,
    date: '2026-06-09T10:00:00.000Z',
    type: 'meeting',
    roles: ['teacher'],
    organizationId: 'org-1',
    campusId: 'campus-1',
    createdById: 'user-1',
    updatedById: null,
    createdAt: '2026-06-09T10:00:00.000Z',
    updatedAt: '2026-06-09T10:00:00.000Z',
  };
}

describe('dashboard selectors', () => {
  it('selects greeting by day segment', () => {
    expect(getDashboardGreeting(new Date('2026-06-09T08:00:00'))).toBe('Good Morning');
    expect(getDashboardGreeting(new Date('2026-06-09T13:00:00'))).toBe('Good Afternoon');
    expect(getDashboardGreeting(new Date('2026-06-09T18:00:00'))).toBe('Good Evening');
  });

  it('calculates day of year and rotates quotes', () => {
    const date = new Date('2026-01-02T12:00:00');

    expect(getDashboardDayOfYear(date)).toBe(2);
    expect(selectDashboardQuote(quotes, date)).toEqual(quotes[0]);
  });

  it('returns null quote when no quotes are published', () => {
    expect(selectDashboardQuote([], new Date('2026-01-02T12:00:00'))).toBeNull();
  });

  it('normalizes dashboard zone values', () => {
    expect(toDashboardZoneColor('green')).toBe('green');
    expect(toDashboardZoneColor('purple')).toBeNull();
    expect(toDashboardZoneColor(null)).toBeNull();
  });

  it('prefers local zone check-in over saved zone', () => {
    expect(selectDashboardActiveZone('yellow', 'green')).toBe('yellow');
    expect(selectDashboardActiveZone(null, 'green')).toBe('green');
  });

  it('limits upcoming events', () => {
    expect(selectDashboardUpcomingEvents([
      createEvent('1'),
      createEvent('2'),
      createEvent('3'),
      createEvent('4'),
      createEvent('5'),
    ])).toHaveLength(4);
  });

  it('hides classroom quick action for office role', () => {
    expect(selectDashboardQuickActions('office_manager').some((action) => action.module === 'classroom')).toBe(false);
    expect(selectDashboardQuickActions('teacher').some((action) => action.module === 'classroom')).toBe(true);
  });
});
