import type {
  CommunicationEventType,
  CommunicationEventDto,
  ParentMessageCategory,
} from '@/shared/types/communications';
import type { UserRole } from '@/shared/types/app';
import { COMMUNICATION_EVENT_TYPES } from '@/shared/constants/communications';

export function canCreateCommunicationEvents(userRole: UserRole): boolean {
  return userRole === 'director' || userRole === 'superintendent';
}

export function getTemplateCategory(
  selectedTemplateId: string | null,
  templates: readonly { readonly id: string; readonly category: ParentMessageCategory }[],
): ParentMessageCategory {
  const template = templates.find((item) => item.id === selectedTemplateId);
  return template?.category ?? 'general';
}

export function filterCommunicationEventsByRole(
  events: readonly CommunicationEventDto[],
  userRole: UserRole,
): readonly CommunicationEventDto[] {
  return events.filter((event) => event.roles.includes(userRole));
}

export function toCommunicationEventType(value: string): CommunicationEventType {
  return COMMUNICATION_EVENT_TYPES.find((type) => type === value) ?? 'meeting';
}
