// src/app/lib/localization.ts

export type PrescriptionLanguage = "English" | "Gujarati" | "Hindi";

const STRINGS: Record<PrescriptionLanguage, Record<string, string>> = {
    English: {
        Age: "Age",
        Weight: "Weight",
        BP: "BP",
        Pulse: "Pulse",
        Temp: "Temp",
        SpO2: "SpO2",
        Symptoms: "Symptoms",
        Diagnosis: "Diagnosis",
        Investigation: "Investigation",
        "Instructions/Advices": "Instructions/Advices",
        Tabs: "Tabs",
        "Before Meal": "Before Meal",
        "After Meal": "After Meal",
        "Injection (IM)": "Injection (IM)",
        "Injection (IV)": "Injection (IV)",
        tablet_prescription: "Tab(s)",
        syrup_inj_prescription: "ml",
        drops_prescription: "Drop(s)",
        surgical_prescription: "Item(s)",
        followup_sentence: "Mark your calendars for %@, for follow-up.",
        patient_weight_text: "kg",
    },
    Gujarati: {
        Age: "ઉંમર",
        Weight: "વજન",
        BP: "બ્લડ પ્રેશર",
        Pulse: "પલ્સ",
        Temp: "તાપમાન",
        SpO2: "SpO2",
        Symptoms: "લક્ષણો",
        Diagnosis: "નિદાન",
        Investigation: "તપાસ",
        "Instructions/Advices": "સૂચનાઓ",
        Tabs: "ગોળી",
        "Before Meal": "જમ્યા પહેલા",
        "After Meal": "જમ્યા પછી",
        "Injection (IM)": "ઇન્જેક્શન (IM)",
        "Injection (IV)": "ઇન્જેક્શન (IV)",
        tablet_prescription: "ગોળી",
        syrup_inj_prescription: "ml",
        drops_prescription: "ટીપાં",
        surgical_prescription: "નંગ",
        followup_sentence: "%@ ના રોજ બતાવવા આવવું.",
        patient_weight_text: "કિગ્રા",
    },
    Hindi: {
        Age: "आयु",
        Weight: "वजन",
        BP: "बीपी",
        Pulse: "पल्स",
        Temp: "तापमान",
        SpO2: "SpO2",
        Symptoms: "लक्षण",
        Diagnosis: "निदान",
        Investigation: "जांच",
        "Instructions/Advices": "निर्देश",
        Tabs: "गोली",
        "Before Meal": "खाने से पहले",
        "After Meal": "खाने के बाद",
        "Injection (IM)": "इंजेक्शन (IM)",
        "Injection (IV)": "इंजेक्शन (IV)",
        tablet_prescription: "गोली",
        syrup_inj_prescription: "ml",
        drops_prescription: "बूँदें",
        surgical_prescription: "नग",
        followup_sentence: "%@ को दिखाने आएं।",
        patient_weight_text: "किग्रा",
    },
};

function normalizeLang(lang?: string): PrescriptionLanguage {
    if (lang === "Gujarati" || lang === "Hindi") return lang;
    return "English";
}

/** iOS na loc(key, bundle) nu web equivalent */
export function loc(key: string, lang?: string): string {
    const L = normalizeLang(lang);
    return STRINGS[L][key] ?? STRINGS.English[key] ?? key;
}

export function formatFollowUpDate(
    followUpDate: string,
    language?: string
): string {
    if (!followUpDate) return "";

    const date = new Date(followUpDate);

    if (isNaN(date.getTime())) {
        return followUpDate;
    }

    const locale =
        language === "Gujarati"
            ? "gu-IN"
            : language === "Hindi"
                ? "hi-IN"
                : "en-IN";

    const weekday = new Intl.DateTimeFormat(locale, {
        weekday: "long",
    }).format(date);

    const day = String(date.getDate()).padStart(2, "0");
    const month = String(date.getMonth() + 1).padStart(2, "0");
    const year = date.getFullYear();

    return `${day}/${month}/${year} (${weekday})`;
}

/** iOS na buildLabels(bundle:followUpDate:) nu equivalent */
export function buildLabels(
    language: string | undefined,
    followUpDate: string
) {
    const formattedFollowUpDate = formatFollowUpDate(
        followUpDate,
        language
    );

    const followupLabel = formattedFollowUpDate
        ? loc("followup_sentence", language).replace(
            "%@",
            formattedFollowUpDate
        )
        : "";

    return {
        age_label: `${loc("Age", language)}:`,
        weight_label: `${loc("Weight", language)}:`,
        bp_label: `${loc("BP", language)}:`,
        pulse_label: `${loc("Pulse", language)}:`,
        spo2_label: `${loc("SpO2", language)}:`,
        temp_label: `${loc("Temp", language)}:`,
        symptoms_label: `${loc("Symptoms", language)}:`,
        diagnosis_label: `${loc("Diagnosis", language)}:`,
        investigation_label: `${loc("Investigation", language)}:`,
        instruction_advice_label: `${loc("Instructions/Advices", language)}:`,
        followup_label: followupLabel,
        quantity_label: loc("Tabs", language),
    };
}

const MEDICINE_TYPE_DOSE_KEY: Record<string, string> = {
    Tablet: "tablet_prescription",
    Capsule: "tablet_prescription",
    Powder: "tablet_prescription",
    Syrup: "syrup_inj_prescription",
    Injection: "syrup_inj_prescription",
    Drops: "drops_prescription",
    Oint: "surgical_prescription",
    "Surg. Items": "surgical_prescription",
};

/** iOS na getDoseSuffix(for:bundle:) nu equivalent */
export function getDoseSuffix(type: string, language?: string): string {
    const key = MEDICINE_TYPE_DOSE_KEY[type] ?? "tablet_prescription";
    return loc(key, language);
}

/** iOS na formatMealMethod(_:bundle:) nu equivalent */
export function formatMealMethod(method: string, language?: string): string {
    const value = (method ?? "").trim();
    switch (value) {
        case "Before Meal":
        case "After Meal":
            return loc(value, language);
        case "IM":
            return loc("Injection (IM)", language);
        case "IV":
            return loc("Injection (IV)", language);
        default:
            return loc("After Meal", language);
    }
}


