
import type { NcStatus, NcSource, PreviousNcRow  } from '../types/previous-nc.types';
// maps a column key → display value for a PreviousNcRow
export function mapColumnKeyToValue(key: string, row: PreviousNcRow): any {
  switch (key) {
    case 'nc_code':      return `NC-${row.source}-${String(row.id).padStart(6, '0')}`;
    case 'company_name': return row.company_name ?? '—';
    case 'created_at':   return row.created_at;
    case 'nc_type':      return row.nc_type;
    case 'created_by':   return row.created_by_name;
    case 'assigned_to':  return row.assigned_to_name;
    case 'status':       return row.status;
    case 'audit_type':   return row.audit_type;
    default:             return (row as any)[key] ?? '—';
  }
}
// ─── Status pill config (matches STATUS_CONFIG from my-audits) ─────────
export const NC_STATUS_CONFIG: Record<
  NcStatus | 'unknown',
  { bg: string; text: string; dot: string; label: string }
> = {
  open: { bg: '#fef2f2', text: '#991b1b', dot: '#dc2626', label: 'Open' },
  closed: { bg: '#f0fdf4', text: '#166534', dot: '#16a34a', label: 'Closed' },
  unknown: { bg: '#f3f4f6', text: '#4b5563', dot: '#94a3b8', label: 'Unknown' },
};

// ─── NC Type pill config (Major / Minor / Observation) ──────────────────
export const NC_TYPE_CONFIG: Record<
  string,
  { bg: string; color: string; icon: string; label: string }
> = {
  Major: { bg: '#fef2f2', color: '#dc2626', icon: '🔴', label: 'Major' },
  Minor: { bg: '#fffbeb', color: '#d97706', icon: '🟡', label: 'Minor' },
  Observation: { bg: '#eff6ff', color: '#2563eb', icon: '🔵', label: 'Observation' },
};

export function getNcTypeMeta(t: string | null | undefined) {
  const v = (t || '').toLowerCase();
  if (v.includes('major')) return NC_TYPE_CONFIG.Major;
  if (v.includes('minor')) return NC_TYPE_CONFIG.Minor;
  if (v.includes('observ')) return NC_TYPE_CONFIG.Observation;
  return { bg: '#f3f4f6', color: '#4b5563', icon: '⚪', label: t || '—' };
}

// ─── Source pill config (QRS / TQS) ─────────────────────────────────────
export const NC_SOURCE_CONFIG: Record<
  NcSource,
  { bg: string; color: string; label: string }
> = {
  QRS: { bg: '#eef2ff', color: '#4338ca', label: 'QRS' },
  TQS: { bg: '#ecfeff', color: '#0891b2', label: 'TQS' },
  NEW: { bg: '#ecfeff', color: '#4338ca', label: 'NEW' },
};

// ─── Date formatter (mirrors formatDateWithHint) ───────────────────────
export function formatDateWithHint(d?: string | Date | null): {
  display: string;
  hint?: string;
  hintColor?: string;
} {
  if (!d) return { display: '—' };
  const dt = typeof d === 'string' ? new Date(d) : d;
  if (isNaN(dt.getTime())) return { display: '—' };

  const display = dt.toLocaleDateString('en-GB', {
    day: '2-digit',
    month: 'short',
    year: '2-digit',
  });

  const sec = Math.floor((Date.now() - dt.getTime()) / 1000);
  let hint = '';
  let hintColor: string | undefined;

  if (sec < 60) {
    hint = 'just now';
    hintColor = '#0f766e';
  } else if (sec < 3600) {
    hint = `${Math.floor(sec / 60)}m ago`;
    hintColor = '#0f766e';
  } else if (sec < 86400) {
    hint = `${Math.floor(sec / 3600)}h ago`;
    hintColor = '#0f766e';
  } else if (sec < 7 * 86400) {
    hint = `${Math.floor(sec / 86400)}d ago`;
    hintColor = '#0f766e';
  } else if (sec < 30 * 86400) {
    hint = `${Math.floor(sec / 86400)}d ago`;
  } else if (sec < 365 * 86400) {
    hint = `${Math.floor(sec / (30 * 86400))}mo ago`;
  } else {
    hint = `${Math.floor(sec / (365 * 86400))}y ago`;
  }

  return { display, hint, hintColor };
}

// ─── Avatar gradient hash (matches MyAuditRow gradients) ───────────────
const AVATAR_GRADIENTS = [
  'linear-gradient(135deg, #7c3aed 0%, #a855f7 100%)', // purple — coordinator-like
  'linear-gradient(135deg, #0f766e 0%, #14b8a6 100%)', // teal — lead-auditor-like
  'linear-gradient(135deg, #2563eb 0%, #60a5fa 100%)', // blue
  'linear-gradient(135deg, #db2777 0%, #f472b6 100%)', // pink
  'linear-gradient(135deg, #ea580c 0%, #fb923c 100%)', // orange
  'linear-gradient(135deg, #4f46e5 0%, #818cf8 100%)', // indigo
];

export function avatarGradient(name: string | null | undefined): string {
  if (!name) return AVATAR_GRADIENTS[0];
  let h = 0;
  for (let i = 0; i < name.length; i++) h = (h << 5) - h + name.charCodeAt(i);
  return AVATAR_GRADIENTS[Math.abs(h) % AVATAR_GRADIENTS.length];
}

export function firstInitial(name: string | null | undefined): string {
  if (!name) return '?';
  return name.trim().charAt(0).toUpperCase() || '?';
}

export function formatUserName(name: string | null | undefined): string {
  return (name || '').trim() || 'N/A';
}
