/**
 * Time management TypeScript types — time-management.
 * Shared between API handlers and React components.
 */

// ── String literal unions ─────────────────────────────────────────────────────

export type TimeEntrySource = 'manual' | 'magic_link' | 'auto' | 'contractor_portal'

export type TimeRoundingMode =
  | 'none'
  | 'nearest_5'
  | 'nearest_15'
  | 'nearest_30'
  | 'up_15'
  | 'up_30'

export type MagicLinkPurpose =
  | 'timer'
  | 'portal'
  | 'portal_invite'
  | 'portal_password_reset'
  | 'pending_2fa'
  | 'pending_2fa_setup'

export type TimeApprovalStatus =
  | 'auto_approved'
  | 'pending'
  | 'approved'
  | 'rejected'
  | 'locked'

// ── Domain objects (JSON-serialized, dates as ISO strings) ────────────────────

export interface TimeEntry {
  id: string
  tenantId: string
  userId: string | null
  contractorId: string | null
  taskId: string | null
  projectId: string
  description: string | null
  startedAt: string
  stoppedAt: string | null
  durationSeconds: number | null
  source: TimeEntrySource
  billable: boolean
  invoiceId?: string | null
  billedAt?: string | null
  approvalStatus: TimeApprovalStatus
  lockedAt: string | null
  lockedReason: string | null
  createdAt: string
  updatedAt: string
}

export interface TimeEntryObject extends TimeEntry {
  projectName: string
  taskTitle: string | null
  userName: string | null
}

export interface WeekDaySummary {
  date: string         // ISO date 'YYYY-MM-DD'
  totalSeconds: number
}

export interface WeekSummary {
  week: string         // 'YYYY-WNN'
  days: WeekDaySummary[]
  totalSeconds: number
}

export interface TimerState {
  entryId: string
  startedAt: string
  projectId: string
  projectName: string
  taskId: string | null
  taskName: string | null
}

// ── Rounding config ───────────────────────────────────────────────────────────

export const TIME_ROUNDING_MODES: readonly TimeRoundingMode[] = [
  'none',
  'nearest_5',
  'nearest_15',
  'nearest_30',
  'up_15',
  'up_30',
] as const

export const TIME_ROUNDING_LABELS: Record<TimeRoundingMode, { he: string; en: string }> = {
  none: { he: 'ללא עיגול', en: 'No rounding (raw seconds)' },
  nearest_5: { he: 'ל-5 הדקות הקרובות', en: 'Nearest 5 minutes' },
  nearest_15: { he: 'ל-15 הדקות הקרובות', en: 'Nearest 15 minutes' },
  nearest_30: { he: 'ל-30 הדקות הקרובות', en: 'Nearest 30 minutes' },
  up_15: { he: "עיגול מעלה (15 דק')", en: 'Always round up (15 min)' },
  up_30: { he: "עיגול מעלה (30 דק')", en: 'Always round up (30 min)' },
}

// ── API request/response shapes ───────────────────────────────────────────────

export interface StartTimerInput {
  projectId: string
  taskId?: string | null
  description?: string | null
  source?: TimeEntrySource
  startedAt?: string
}

export interface ManualEntryInput {
  projectId: string
  taskId?: string | null
  startedAt: string
  stoppedAt: string
  description?: string | null
  billable?: boolean
}

export interface TimeTrackingSettings {
  time_rounding: TimeRoundingMode
  time_min_billable_minutes: number
  time_idle_threshold_minutes: number
  time_auto_pause_on_idle: boolean
  time_standard_hours_per_day: number
  time_flag_overtime: boolean
  time_require_overtime_approval: boolean
  contractor_time_enabled: boolean
  mileage_enabled: boolean
  time_magic_link_enabled: boolean
}

export interface PaginatedResponse<T> {
  items: T[]
  nextCursor: string | null
  total: number
}
