/**
 * Locale-preferenser (i18n-spec §6): språk ≠ region ≠ måttsystem ≠ valuta.
 * En svensk i USA kan ha svensk UI-text, amerikansk region, USD och Fahrenheit.
 * Standarder: BCP 47 (språk), ISO 3166-1 alpha-2 (land), IANA (tidszon), ISO 4217 (valuta).
 */

export const MEASUREMENT_SYSTEMS = ["METRIC", "US_CUSTOMARY", "MIXED"] as const;
export type MeasurementSystem = (typeof MEASUREMENT_SYSTEMS)[number];

export const TEMPERATURE_UNITS = ["CELSIUS", "FAHRENHEIT"] as const;
export type TemperatureUnit = (typeof TEMPERATURE_UNITS)[number];

export interface UserLocalePreferences {
  languageTag: string; // BCP 47, t.ex. "sv-SE"
  regionCode: string; // ISO 3166-1 alpha-2, t.ex. "SE"
  timeZone: string; // IANA, t.ex. "Europe/Stockholm"
  measurementSystem: MeasurementSystem;
  temperatureUnit: TemperatureUnit;
  currencyCode: string; // ISO 4217, t.ex. "SEK"
  firstDayOfWeek: number; // 0 = söndag … 6 = lördag
  use24HourTime: boolean;
}

/** Kontext som följer med varje AAMOS-anrop (i18n-spec §22). */
export interface LocaleContext {
  languageTag: string;
  regionCode: string;
  timeZone: string;
  measurementSystem: MeasurementSystem;
  temperatureUnit: TemperatureUnit;
  currencyCode: string;
}

export const DEFAULT_LOCALE_PREFERENCES: UserLocalePreferences = {
  languageTag: "sv-SE",
  regionCode: "SE",
  timeZone: "Europe/Stockholm",
  measurementSystem: "METRIC",
  temperatureUnit: "CELSIUS",
  currencyCode: "SEK",
  firstDayOfWeek: 1,
  use24HourTime: true,
};

/** Rimliga regiondefaults – användaren kan alltid ändra varje fält separat. */
const REGION_DEFAULTS: Record<string, Partial<UserLocalePreferences>> = {
  SE: {},
  NO: { languageTag: "nb-NO", timeZone: "Europe/Oslo", currencyCode: "NOK" },
  DK: { languageTag: "da-DK", timeZone: "Europe/Copenhagen", currencyCode: "DKK" },
  FI: { languageTag: "fi-FI", timeZone: "Europe/Helsinki", currencyCode: "EUR" },
  IS: { languageTag: "is-IS", timeZone: "Atlantic/Reykjavik", currencyCode: "ISK" },
  ES: { languageTag: "es-ES", timeZone: "Europe/Madrid", currencyCode: "EUR" },
  NL: { languageTag: "nl-NL", timeZone: "Europe/Amsterdam", currencyCode: "EUR" },
  PL: { languageTag: "pl-PL", timeZone: "Europe/Warsaw", currencyCode: "PLN" },
  PT: { languageTag: "pt-PT", timeZone: "Europe/Lisbon", currencyCode: "EUR" },
  IT: { languageTag: "it-IT", timeZone: "Europe/Rome", currencyCode: "EUR" },
  GB: {
    languageTag: "en-GB",
    timeZone: "Europe/London",
    currencyCode: "GBP",
    measurementSystem: "MIXED",
  },
  US: {
    languageTag: "en-US",
    timeZone: "America/New_York",
    currencyCode: "USD",
    measurementSystem: "US_CUSTOMARY",
    temperatureUnit: "FAHRENHEIT",
    firstDayOfWeek: 0,
    use24HourTime: false,
  },
  CA: {
    languageTag: "en-CA",
    timeZone: "America/Toronto",
    currencyCode: "CAD",
    measurementSystem: "MIXED",
    firstDayOfWeek: 0,
    use24HourTime: false,
  },
  DE: { languageTag: "de-DE", timeZone: "Europe/Berlin", currencyCode: "EUR" },
  FR: { languageTag: "fr-FR", timeZone: "Europe/Paris", currencyCode: "EUR" },
};

export function localeDefaultsForRegion(regionCode: string): UserLocalePreferences {
  const overrides = REGION_DEFAULTS[regionCode.toUpperCase()] ?? {};
  return { ...DEFAULT_LOCALE_PREFERENCES, regionCode: regionCode.toUpperCase(), ...overrides };
}

export function toLocaleContext(prefs: UserLocalePreferences): LocaleContext {
  return {
    languageTag: prefs.languageTag,
    regionCode: prefs.regionCode,
    timeZone: prefs.timeZone,
    measurementSystem: prefs.measurementSystem,
    temperatureUnit: prefs.temperatureUnit,
    currencyCode: prefs.currencyCode,
  };
}

/** Temperaturkonvertering för visning (canonical lagras alltid i Celsius). */
export function celsiusToDisplay(celsius: number, unit: TemperatureUnit): number {
  return unit === "FAHRENHEIT" ? Math.round((celsius * 9) / 5 + 32) : celsius;
}
