import type { AbsenceSpan } from '../types/absence'

export const MONTHS = [
  'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni',
  'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember',
]

export const MONTHS_SHORT = [
  'Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun',
  'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez',
]

export const DAY_LABELS = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So']

export const STATUS_LABELS: Record<string, string> = {
  pending: 'Ausstehend',
  approved: 'Genehmigt',
  rejected: 'Abgelehnt',
  cancelled: 'Storniert',
  cancellation_requested: 'Storno beantragt',
  first_approved: 'Wartet auf Endgenehmigung',
}

export const STATUS_COLORS: Record<string, string> = {
  pending: 'bg-yellow-100 text-yellow-700',
  approved: 'bg-green-100 text-green-700',
  rejected: 'bg-red-100 text-red-700',
  cancelled: 'bg-gray-100 text-gray-500',
  cancellation_requested: 'bg-orange-100 text-orange-700',
  first_approved: 'bg-sky-100 text-sky-700',
}

export const MANAGER_ROLES = ['COMPANY_ADMIN', 'SUPER_ADMIN', 'HR', 'MANAGER']

export function isoWeekday(d: Date): number {
  return (d.getDay() + 6) % 7
}

export function buildCalendarWeeks(year: number, month: number): Date[][] {
  const first = new Date(year, month, 1)
  const last = new Date(year, month + 1, 0)
  const weeks: Date[][] = []
  let week: Date[] = []

  // Pad from Monday to the first day of the month
  const startWd = isoWeekday(first)
  for (let i = 0; i < startWd; i++) {
    const d = new Date(year, month, 1 - (startWd - i))
    week.push(d)
  }

  for (let day = 1; day <= last.getDate(); day++) {
    week.push(new Date(year, month, day))
    if (week.length === 7) {
      weeks.push(week)
      week = []
    }
  }

  // Pad remaining days to complete last week
  if (week.length > 0) {
    let next = 1
    while (week.length < 7) {
      week.push(new Date(year, month + 1, next++))
    }
    weeks.push(week)
  }

  return weeks
}

export function getWeekSpans(week: Date[], spans: AbsenceSpan[]): AbsenceSpan[] {
  const weekStart = week[0]
  const weekEnd = week[week.length - 1]
  return spans.filter(s => s.start <= weekEnd && s.end >= weekStart)
}
