// Formatting utilities for displaying metrics
export const formatCurrency = (value: number | null | undefined, digits = 2): string => {
    if (value == null) return 'N/A';
    return new Intl.NumberFormat('en-US', {
        style: 'currency',
        currency: 'USD',
        minimumFractionDigits: digits,
        maximumFractionDigits: digits
    }).format(value);
};

export const formatNumber = (value: number | null | undefined, digits = 0): string => {
    if (value == null) return 'N/A';
    return value.toLocaleString(undefined, {
        minimumFractionDigits: digits,
        maximumFractionDigits: digits
    });
};

export const formatPercentage = (value: number | null | undefined, digits = 1): string => {
    if (value == null) return 'N/A';
    return `${value.toFixed(digits)}%`;
};

export const formatDays = (value: number | null | undefined, digits = 0): string => {
    if (value == null) return 'N/A';
    return `${value.toFixed(digits)} days`;
};

export const formatDate = (dateString: string | null | undefined): string => {
    if (!dateString) return 'N/A';
    try {
        return new Date(dateString).toLocaleDateString('en-US', {
            year: 'numeric',
            month: 'short',
            day: 'numeric'
        });
    } catch (e) {
        return 'Invalid Date';
    }
};

export const formatDateShort = (dateString: string | null | undefined): string => {
    if (!dateString) return 'N/A';
    try {
        return new Date(dateString).toLocaleDateString('en-US', {
            year: 'numeric',
            month: 'numeric',
            day: 'numeric'
        });
    } catch (e) {
        return 'Invalid Date';
    }
};

export const formatBoolean = (value: boolean | null | undefined): string => {
    if (value == null) return 'N/A';
    return value ? 'Yes' : 'No';
};
