/**
 * Core Guards
 * 
 * Lightweight runtime validators for core types.
 * These are JSON-compatible and serialization-safe.
 * 
 * @author Matthew Raymer
 * @version 1.0.0
 */

import type { ScheduleKind, HistoryKind, HistoryOutcome, CallbackKind } from './enums';

/**
 * Type guard: Check if value is a valid ScheduleKind
 */
export function isScheduleKind(value: unknown): value is ScheduleKind {
  return value === 'fetch' || value === 'notify';
}

/**
 * Type guard: Check if value is a valid HistoryKind
 */
export function isHistoryKind(value: unknown): value is HistoryKind {
  return (
    value === 'fetch' ||
    value === 'notify' ||
    value === 'callback' ||
    value === 'boot_recovery'
  );
}

/**
 * Type guard: Check if value is a valid HistoryOutcome
 */
export function isHistoryOutcome(value: unknown): value is HistoryOutcome {
  return (
    value === 'success' ||
    value === 'failure' ||
    value === 'skipped_ttl' ||
    value === 'circuit_open'
  );
}

/**
 * Type guard: Check if value is a valid CallbackKind
 */
export function isCallbackKind(value: unknown): value is CallbackKind {
  return value === 'http' || value === 'local' || value === 'queue';
}

/**
 * Validate time format (HH:mm)
 */
export function isValidTimeFormat(time: string): boolean {
  const timeRegex = /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/;
  if (!timeRegex.test(time)) {
    return false;
  }
  const [hours, minutes] = time.split(':').map(Number);
  return hours >= 0 && hours <= 23 && minutes >= 0 && minutes <= 59;
}

/**
 * Validate timestamp (milliseconds since epoch)
 */
export function isValidTimestamp(timestamp: unknown): timestamp is number {
  return (
    typeof timestamp === 'number' &&
    !isNaN(timestamp) &&
    timestamp > 0 &&
    timestamp < Number.MAX_SAFE_INTEGER
  );
}

