import React from 'react';
import { formatCalendarDateEs } from '../../utils/serverCalendarDate';

function isBotUpdated(value: unknown): boolean {
  return value === true || value === 1 || value === '1';
}

function botLabel(date?: string | null): string {
  if (!date) {
    return 'Actualizado por el robot de fechas';
  }

  const raw = String(date).trim().replace('T', ' ');
  const day = formatCalendarDateEs(raw);
  const time = raw.match(/\d{2}:\d{2}/)?.[0];

  return time && day !== '-'
    ? `Actualizado por el robot de fechas el ${day} a las ${time}`
    : `Actualizado por el robot de fechas el ${day}`;
}

type Props = {
  updated?: unknown;
  date?: string | null;
};

const BotUpdateIcon: React.FC<Props> = ({ updated, date }) => {
  if (!isBotUpdated(updated)) {
    return null;
  }

  const label = botLabel(date);

  return (
    <i
      className="las la-robot"
      title={label}
      aria-label={label}
      style={{ color: '#0f766e', fontSize: '1.15rem', lineHeight: 1 }}
    />
  );
};

export default BotUpdateIcon;
