export function formatSensorMetricValue(key: string, value: unknown): string {
    if (typeof value !== 'number') {
        return typeof value === 'object' ? JSON.stringify(value) : String(value);
    }

    switch (key) {
        case 'wind_speed': {
            const milesPerHour = value * 2.2369362921;

            return `${trimTrailingZeros(value)} m/s (${trimTrailingZeros(milesPerHour)} mph)`;
        }
        case 'temperature':
            return `${trimTrailingZeros(value)} °C`;
        case 'battery':
            return `${trimTrailingZeros(value)}%`;
        case 'pressure':
            return `${trimTrailingZeros(value)} hPa`;
        case 'rainfall_total':
            return `${trimTrailingZeros(value)} mm`;
        case 'humidity':
            return `${trimTrailingZeros(value)} RH`;
        default:
            return trimTrailingZeros(value);
    }
}

function trimTrailingZeros(value: number): string {
    return Number.isInteger(value) ? String(value) : value.toFixed(2).replace(/\.?0+$/, '');
}
