/**
 * Local formatters for the classrooms/meetings feature area.
 *
 * Mirrors the helpers expected from a future shared `resources/js/lib/format.ts`
 * (formatEuro, formatDateTime, formatDate, formatMinutes) so this feature can
 * compile independently until that file lands.
 */

export function formatEuro(amount: number): string {
    return new Intl.NumberFormat('it-IT', {
        style: 'currency',
        currency: 'EUR',
    }).format(amount);
}

export function formatDate(value: string | null | undefined): string {
    if (!value) {
        return '—';
    }

    return new Intl.DateTimeFormat('it-IT', { dateStyle: 'medium' }).format(
        new Date(value),
    );
}

export function formatDateTime(value: string | null | undefined): string {
    if (!value) {
        return '—';
    }

    return new Intl.DateTimeFormat('it-IT', {
        dateStyle: 'medium',
        timeStyle: 'short',
    }).format(new Date(value));
}

export function formatMinutes(totalMinutes: number): string {
    const minutes = Math.max(0, Math.round(totalMinutes));
    const hours = Math.floor(minutes / 60);
    const mins = minutes % 60;

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

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

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

export function formatSeconds(totalSeconds: number): string {
    return formatMinutes(totalSeconds / 60);
}

/** Converts a `datetime-local` input value <-> ISO string without timezone surprises. */
export function toDatetimeLocalValue(value: string | null | undefined): string {
    if (!value) {
        return '';
    }

    const date = new Date(value);
    const pad = (n: number) => String(n).padStart(2, '0');
    const y = date.getFullYear();
    const m = pad(date.getMonth() + 1);
    const d = pad(date.getDate());
    const h = pad(date.getHours());
    const min = pad(date.getMinutes());

    return `${y}-${m}-${d}T${h}:${min}`;
}
