import { hasPermission } from '@/lib/auth/authorization';
import type { Announcement } from '../api/types';

export function canManageAnnouncementPin(permissions: readonly string[] | null | undefined) {
  return hasPermission(permissions, 'announcement:publish');
}

export function isAnnouncementPinEligible(announcement: Pick<Announcement, 'status' | 'start_date' | 'end_date'>) {
  if (announcement.status !== 'published') {
    return false;
  }

  const now = new Date();
  const startDate = new Date(announcement.start_date);

  if (startDate > now) {
    return false;
  }

  if (announcement.end_date) {
    const endDate = new Date(announcement.end_date);

    if (endDate < now) {
      return false;
    }
  }

  return true;
}
