const euroFormatter = new Intl.NumberFormat('it-IT', {
    style: 'currency',
    currency: 'EUR',
});

const dateTimeFormatter = new Intl.DateTimeFormat('it-IT', {
    day: '2-digit',
    month: '2-digit',
    year: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
});

const dateFormatter = new Intl.DateTimeFormat('it-IT', {
    day: '2-digit',
    month: '2-digit',
    year: 'numeric',
});

/** Formats an amount in cents as a euro currency string, e.g. 129900 -> "1.299,00 €". */
export function formatEuro(cents: number): string {
    return euroFormatter.format(cents / 100);
}

/** Formats an ISO date-time string in Italian format, e.g. "12/03/2026, 09:30". */
export function formatDateTime(iso: string | null | undefined): string {
    if (!iso) {
        return '—';
    }

    return dateTimeFormatter.format(new Date(iso));
}

/** Formats an ISO date string in Italian format, e.g. "12/03/2026". */
export function formatDate(iso: string | null | undefined): string {
    if (!iso) {
        return '—';
    }

    return dateFormatter.format(new Date(iso));
}

const hoursFormatter = new Intl.NumberFormat('it-IT', {
    maximumFractionDigits: 1,
});

/** Formats a number of hours in Italian notation, e.g. 4.5 -> "4,5". */
export function formatHours(hours: number | null | undefined): string {
    if (hours === null || hours === undefined) {
        return '—';
    }

    return hoursFormatter.format(hours);
}

/** Formats a duration in minutes as a human readable string, e.g. 150 -> "2h 30min". */
export function formatMinutes(minutes: number | null | undefined): string {
    if (minutes === null || minutes === undefined) {
        return '—';
    }

    const hours = Math.floor(minutes / 60);
    const remainingMinutes = minutes % 60;

    if (hours === 0) {
        return `${remainingMinutes}min`;
    }

    if (remainingMinutes === 0) {
        return `${hours}h`;
    }

    return `${hours}h ${remainingMinutes}min`;
}
