// Format currency from micros to display string (INR)
export const formatCurrency = (amountMicros: number, currency = 'INR'): string => {
    const amount = amountMicros / 1_000_000;
    return new Intl.NumberFormat('en-IN', { style: 'currency', currency, maximumFractionDigits: 0 }).format(amount);
};

// Format phone number for display
export const formatPhone = (phone: { number: string; callingCode: string }): string =>
    `${phone.callingCode} ${phone.number.replace(/(\d{5})(\d{5})/, '$1 $2')}`;

// Calculate days ago from ISO date string
export const daysAgoFromNow = (dateStr: string): number => {
    const diff = Date.now() - new Date(dateStr).getTime();
    return Math.floor(diff / (1000 * 60 * 60 * 24));
};

// Get aging bracket from days
export const getAgeBracket = (days: number): 'fresh' | 'warm' | 'cold' =>
    days < 2 ? 'fresh' : days <= 5 ? 'warm' : 'cold';

// Format relative age string
export const formatRelativeAge = (dateStr: string): string => {
    const days = daysAgoFromNow(dateStr);
    if (days === 0) return 'Today';
    if (days === 1) return '1 day ago';
    return `${days} days ago`;
};

// All date formatting uses browser's local timezone (no hardcoded TZ)
// Timestamps from the API are UTC — Intl.DateTimeFormat converts to local automatically

// Mar 15, 2:30 PM
export const formatShortDate = (dateStr: string): string =>
    new Intl.DateTimeFormat('en-IN', { month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit', hour12: true }).format(new Date(dateStr));

// 15 Mar 2026
export const formatDateOnly = (dateStr: string): string =>
    new Intl.DateTimeFormat('en-IN', { day: 'numeric', month: 'short', year: 'numeric' }).format(new Date(dateStr));

// 2:30 PM
export const formatTimeOnly = (dateStr: string): string =>
    new Intl.DateTimeFormat('en-IN', { hour: 'numeric', minute: '2-digit', hour12: true }).format(new Date(dateStr));

// Mar 15, 2:30 PM (with day + month + time, no year)
export const formatDateTimeShort = (dateStr: string): string =>
    new Intl.DateTimeFormat('en-IN', { day: 'numeric', month: 'short', hour: 'numeric', minute: '2-digit', hour12: true }).format(new Date(dateStr));

// Mon, 15
export const formatWeekdayShort = (dateStr: string): string =>
    new Intl.DateTimeFormat('en-IN', { weekday: 'short', day: 'numeric' }).format(new Date(dateStr));

// 02:30:45 PM
export const formatTimeFull = (dateStr: string): string =>
    new Intl.DateTimeFormat('en-IN', { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true }).format(new Date(dateStr));

// 1st April, 2nd March, etc.
const ordinalSuffix = (n: number): string => {
    const s = ['th', 'st', 'nd', 'rd'];
    const v = n % 100;
    return n + (s[(v - 20) % 10] || s[v] || s[0]);
};

export const formatDateOrdinal = (dateStr: string): string => {
    const d = new Date(dateStr);
    return `${ordinalSuffix(d.getDate())} ${d.toLocaleDateString('en-IN', { month: 'long' })}`;
};

// Get initials from a name
export const getInitials = (firstName: string, lastName: string): string =>
    `${firstName[0] || ''}${lastName[0] || ''}`.toUpperCase();

// Format large numbers (1234 -> "1.2K", 1234567 -> "1.2M")
export const formatCompact = (n: number): string =>
    new Intl.NumberFormat('en-IN', { notation: 'compact', maximumFractionDigits: 1 }).format(n);
