const fleetTranslations = {
    en: {
        "common.language": "Language",
        "fleet.page_title": "Sentinel Fleet",
        "fleet.kicker": "Fleet intelligence",
        "fleet.title": "Equipment overview",
        "fleet.registered": "registered equipment",
        "fleet.platform_online": "Sentinel platform online",
        "fleet.monitoring_active": "Fleet monitoring active",
        "fleet.total": "Total equipment",
        "fleet.excellent": "Excellent",
        "fleet.attention": "Attention required",
        "fleet.assets": "Monitored assets",
        "fleet.equipment_fleet": "Equipment fleet",
        "fleet.freezer": "Laboratory freezer",
        "fleet.health_score": "Health score",
        "fleet.internal": "Internal",
        "fleet.last_measurement": "Last measurement",
        "fleet.open": "Open equipment →",
        "fleet.empty": "No equipment registered",
        "fleet.tagline": "Equipment Intelligence · Before Failure",
        "global.fleet": "Fleet",
        "global.alarms": "Alarms",
        "global.trends": "Trends",
        "global.reports": "Reports",
        "global.events": "Events",
        "global.administration": "Administration",
    },

    fr: {
        "common.language": "Langue",
        "fleet.page_title": "Flotte Sentinel",
        "fleet.kicker": "Intelligence de la flotte",
        "fleet.title": "Vue d’ensemble des équipements",
        "fleet.registered": "équipements enregistrés",
        "fleet.platform_online": "Plateforme Sentinel en ligne",
        "fleet.monitoring_active": "Surveillance de la flotte active",
        "fleet.total": "Total équipements",
        "fleet.excellent": "Excellent",
        "fleet.attention": "À surveiller",
        "fleet.assets": "Équipements surveillés",
        "fleet.equipment_fleet": "Flotte d’équipements",
        "fleet.freezer": "Congélateur de laboratoire",
        "fleet.health_score": "Health Score",
        "fleet.internal": "Température interne",
        "fleet.last_measurement": "Dernière mesure",
        "fleet.open": "Ouvrir l’équipement →",
        "fleet.empty": "Aucun équipement enregistré",
        "fleet.tagline": "Intelligence des équipements · Avant la panne",
        "global.fleet": "Flotte",
        "global.alarms": "Alarmes",
        "global.trends": "Tendances",
        "global.reports": "Rapports",
        "global.events": "Événements",
        "global.administration": "Administration",
    },
};

const statusTranslations = {
    en: {
        EXCELLENT: "Excellent",
        WATCH: "Watch",
        WARNING: "Warning",
        CRITICAL: "Critical",
        OFFLINE: "Offline",
    },
    fr: {
        EXCELLENT: "Excellent",
        WATCH: "Surveillance",
        WARNING: "Alerte",
        CRITICAL: "Critique",
        OFFLINE: "Hors ligne",
    },
};

let fleetLanguage =
    localStorage.getItem("sentinel-language") || "en";


function fleetFormatTimestamp(value) {
    if (!value) {
        return fleetLanguage === "fr"
            ? "Aucune donnée"
            : "No data";
    }

    const normalized = value.includes("T")
        ? value
        : `${value.replace(" ", "T")}Z`;

    const date = new Date(normalized);

    if (Number.isNaN(date.getTime())) {
        return value;
    }

    return new Intl.DateTimeFormat(
        fleetLanguage === "fr"
            ? "fr-FR"
            : "en-GB",
        {
            day: "2-digit",
            month: "short",
            hour: "2-digit",
            minute: "2-digit",
        }
    ).format(date);
}


function applyFleetLanguage(language) {
    fleetLanguage = language;
    localStorage.setItem(
        "sentinel-language",
        language
    );

    document.documentElement.lang = language;

    document
        .querySelectorAll("[data-i18n]")
        .forEach(element => {
            const key = element.dataset.i18n;
            const value =
                fleetTranslations[language]?.[key];

            if (value) {
                element.textContent = value;
            }
        });

    document
        .querySelectorAll("[data-health-status]")
        .forEach(element => {
            const status =
                element.dataset.healthStatus;

            element.textContent =
                statusTranslations[language]?.[status]
                ?? status;
        });

    document
        .querySelectorAll(".fleet-recorded-at")
        .forEach(element => {
            element.textContent =
                fleetFormatTimestamp(
                    element.dataset.recordedAt
                );
        });

    const selector =
        document.getElementById(
            "fleet-language-selector"
        );

    if (selector) {
        selector.value = language;
    }

    document.title =
        fleetTranslations[language]["fleet.page_title"];
}


document
    .getElementById("fleet-language-selector")
    ?.addEventListener(
        "change",
        event => {
            applyFleetLanguage(
                event.target.value
            );
        }
    );


applyFleetLanguage(fleetLanguage);

document.getElementById("fleet-group-filter")?.addEventListener("change", event => {
    const value = event.target.value;
    window.location.href = value ? `/?group=${encodeURIComponent(value)}` : "/";
});
