import type { RowStatus, AuditType } from '../types/my-audits.types';

// ─────────────────────────────────────────────────────────────────────────────
// Status pill colors — same look as ROW_STATUS_META in audit-schedules.
// `dot` is the bar/dot color, `bg` is the pill background, `text` is the text.
// ─────────────────────────────────────────────────────────────────────────────
export const STATUS_CONFIG: Record<
  RowStatus,
  { label: string; bg: string; text: string; dot: string }
> = {
  PENDING: {
    label: 'Pending',
    bg: '#fef3c7',
    text: '#92400e',
    dot: '#f59e0b',
  },
  CONFIRMED: {
    label: 'Scheduled',
    bg: '#dcfce7',
    text: '#166534',
    dot: '#22c55e',
  },
  IN_PROGRESS: {
    label: 'In Progress',
    bg: '#ede9fe',
    text: '#5b21b6',
    dot: '#8b5cf6',
  },
  COMPLETED: {
    label: 'Completed',
    bg: '#d1fae5',
    text: '#065f46',
    dot: '#10b981',
  },
  CANCELLED: {
    label: 'Cancelled',
    bg: '#fee2e2',
    text: '#991b1b',
    dot: '#ef4444',
  },
  RESCHEDULED: {
    label: 'Rescheduled',
    bg: '#dbeafe',
    text: '#1e40af',
    dot: '#3b82f6',
  },
};

// ─────────────────────────────────────────────────────────────────────────────
// Audit-type meta — used by the category KPI tiles and donut chart.
// Initial = green, Surveillance = amber, Recert = purple.
// ─────────────────────────────────────────────────────────────────────────────
export const TYPE_CONFIG: Record<
  AuditType,
  { label: string; color: string; bg: string; icon: string }
> = {
  INITIAL: {
    label: 'Initial',
    color: '#65a30d',
    bg: '#f7fee7',
    icon: '🏁',
  },
  SURVEILLANCE: {
    label: 'Surveillance',
    color: '#d97706',
    bg: '#fffbeb',
    icon: '👁',
  },
  RECERTIFICATION: {
    label: 'Re-Certification',
    color: '#7c3aed',
    bg: '#f5f3ff',
    icon: '🔄',
  },
};

// ─────────────────────────────────────────────────────────────────────────────
// Helper — format a user reference into "Firstname Lastname" or fallback email.
// ─────────────────────────────────────────────────────────────────────────────
export const formatUserName = (
  user: { firstName?: string; lastName?: string; email?: string } | undefined,
): string => {
  if (!user) return '—';
  const name = `${user.firstName ?? ''} ${user.lastName ?? ''}`.trim();
  if (name) return name;
  if (user.email) return user.email;
  return '—';
};

// ─────────────────────────────────────────────────────────────────────────────
// Helper — format a date like "20 May 26" with optional proximity hint
// ("Today" / "in 1d" / "1d ago").
// ─────────────────────────────────────────────────────────────────────────────
export const formatDateWithHint = (
  isoDate: string,
): { display: string; hint?: string; hintColor?: string } => {
  const d = new Date(isoDate);
  const day = String(d.getDate()).padStart(2, '0');
  const month = d.toLocaleString('en-US', { month: 'short' });
  const year = String(d.getFullYear()).slice(2);
  const display = `${day} ${month} ${year}`;

  const today = new Date();
  today.setHours(0, 0, 0, 0);
  const target = new Date(isoDate);
  target.setHours(0, 0, 0, 0);
  const diffDays = Math.round(
    (target.getTime() - today.getTime()) / (1000 * 60 * 60 * 24),
  );

  if (diffDays === 0) {
    return { display, hint: 'Today', hintColor: '#0f766e' };
  }
  if (diffDays === 1) {
    return { display, hint: 'tomorrow', hintColor: '#0f766e' };
  }
  if (diffDays > 1 && diffDays <= 7) {
    return { display, hint: `in ${diffDays}d`, hintColor: '#6b7280' };
  }
  if (diffDays === -1) {
    return { display, hint: '1d ago', hintColor: '#9ca3af' };
  }
  if (diffDays < -1 && diffDays >= -7) {
    return { display, hint: `${Math.abs(diffDays)}d ago`, hintColor: '#9ca3af' };
  }
  return { display };
};
