import { useEffect, useState } from "react";
import { Text } from "react-native";
import { COUNTDOWN_INTERVAL_MS } from "@/lib/constants";
import { formatLastUpdated } from "@/lib/timeUtils";

export function LastUpdated({ timestamp }: { timestamp: number | null }) {
  const [now, setNow] = useState(Date.now);

  useEffect(() => {
    const interval = setInterval(() => setNow(Date.now()), COUNTDOWN_INTERVAL_MS);
    return () => clearInterval(interval);
  }, []);

  if (!timestamp) return null;

  return (
    <Text selectable className="text-xs text-neutral-400 dark:text-neutral-500">
      {formatLastUpdated(timestamp, now)}
    </Text>
  );
}
