/**
 * Plattformskonstanter: planer, gratisnivå, enhetskonvertering, allergen-etiketter.
 */
import type { Allergen, SubscriptionPlan, Unit, UnitKind } from "./enums.js";
import { BRAND } from "./brand.js";

// ---------------------------------------------------------------------------
// Prenumerationsplaner (spec §45–46)
// ---------------------------------------------------------------------------

export interface PlanDefinition {
  plan: SubscriptionPlan;
  nameSv: string;
  priceMinorPerMonth: number;
  /** ISO 4217 för fallback-visning; riktiga priser per marknad kommer från butikerna (M8). */
  currency: string;
  maxHouseholdMembers: number;
  /** Fair use – inte "obegränsad AI" (spec §45). */
  aiScansPerMonth: number;
  weekPlanning: boolean;
  advancedNutrition: boolean;
  communityPublish: boolean;
  appleProductId: string;
  googleProductId: string;
}

export const TRIAL_DAYS = 7;

export const PLAN_DEFINITIONS: Record<SubscriptionPlan, PlanDefinition> = {
  free: {
    plan: "free",
    nameSv: "Gratis",
    priceMinorPerMonth: 0,
    currency: "SEK",
    maxHouseholdMembers: 1,
    aiScansPerMonth: 10,
    weekPlanning: false,
    advancedNutrition: false,
    communityPublish: false,
    appleProductId: "",
    googleProductId: "",
  },
  household: {
    plan: "household",
    nameSv: "Household",
    priceMinorPerMonth: 7900,
    currency: "SEK",
    maxHouseholdMembers: 3,
    aiScansPerMonth: 300,
    weekPlanning: true,
    advancedNutrition: true,
    communityPublish: true,
    appleProductId: `${BRAND.iosBundleId}.household_monthly`,
    googleProductId: "household_monthly",
  },
  family: {
    plan: "family",
    nameSv: "Family",
    priceMinorPerMonth: 12900,
    currency: "SEK",
    maxHouseholdMembers: 6,
    aiScansPerMonth: 600,
    weekPlanning: true,
    advancedNutrition: true,
    communityPublish: true,
    appleProductId: `${BRAND.iosBundleId}.family_monthly`,
    googleProductId: "family_monthly",
  },
  large_household: {
    plan: "large_household",
    nameSv: "Large Household",
    priceMinorPerMonth: 16900,
    currency: "SEK",
    maxHouseholdMembers: 12,
    aiScansPerMonth: 1000,
    weekPlanning: true,
    advancedNutrition: true,
    communityPublish: true,
    appleProductId: `${BRAND.iosBundleId}.large_household_monthly`,
    googleProductId: "large_household_monthly",
  },
};

// ---------------------------------------------------------------------------
// Enheter
// ---------------------------------------------------------------------------

export const UNIT_INFO: Record<Unit, { kind: UnitKind; toBase: number }> = {
  GRAM: { kind: "mass", toBase: 1 },
  KILOGRAM: { kind: "mass", toBase: 1000 },
  OUNCE: { kind: "mass", toBase: 28.35 },
  POUND: { kind: "mass", toBase: 453.59 },
  MILLILITER: { kind: "volume", toBase: 1 },
  DECILITER: { kind: "volume", toBase: 100 },
  LITER: { kind: "volume", toBase: 1000 },
  TEASPOON: { kind: "volume", toBase: 5 },
  TABLESPOON: { kind: "volume", toBase: 15 },
  CUP_US: { kind: "volume", toBase: 236.59 },
  FLUID_OUNCE_US: { kind: "volume", toBase: 29.57 },
  PINCH: { kind: "volume", toBase: 0.5 },
  COUNT: { kind: "count", toBase: 1 },
  PORTION: { kind: "count", toBase: 1 },
  SLICE: { kind: "count", toBase: 1 },
  CLOVE: { kind: "count", toBase: 1 },
  CAN: { kind: "count", toBase: 1 },
  PACKAGE: { kind: "count", toBase: 1 },
};

// ---------------------------------------------------------------------------
// Allergener – svenska etiketter (EU:s 14)
// ---------------------------------------------------------------------------

export const ALLERGEN_LABELS_SV: Record<Allergen, string> = {
  gluten: "Gluten",
  crustaceans: "Kräftdjur",
  eggs: "Ägg",
  fish: "Fisk",
  peanuts: "Jordnötter",
  soy: "Soja",
  milk: "Mjölk (laktos)",
  tree_nuts: "Nötter",
  celery: "Selleri",
  mustard: "Senap",
  sesame: "Sesamfrön",
  sulphites: "Sulfiter",
  lupin: "Lupin",
  molluscs: "Blötdjur",
};

// ---------------------------------------------------------------------------
// Bäst före-klassning (deterministisk, spec §13)
// ---------------------------------------------------------------------------

/** Dagar kvar → status. use_soon: ≤ USE_SOON_DAYS, expiring: ≤ EXPIRING_DAYS. */
export const EXPIRY_THRESHOLDS = {
  EXPIRING_DAYS: 2,
  USE_SOON_DAYS: 5,
} as const;

// ---------------------------------------------------------------------------
// Butiksavdelningar för inköpslistans sortering (spec §27)
// ---------------------------------------------------------------------------

export const STORE_SECTIONS = [
  "frukt_gront",
  "brod",
  "mejeri",
  "kott_fagel",
  "fisk",
  "chark",
  "frys",
  "skafferi",
  "konserver",
  "kryddor_bak",
  "dryck",
  "snacks",
  "hygien_ovrigt",
] as const;
export type StoreSection = (typeof STORE_SECTIONS)[number];

export const STORE_SECTION_LABELS_SV: Record<StoreSection, string> = {
  frukt_gront: "Frukt & grönt",
  brod: "Bröd",
  mejeri: "Mejeri",
  kott_fagel: "Kött & fågel",
  fisk: "Fisk & skaldjur",
  chark: "Chark",
  frys: "Frys",
  skafferi: "Skafferi",
  konserver: "Konserver",
  kryddor_bak: "Kryddor & bakning",
  dryck: "Dryck",
  snacks: "Snacks & godis",
  hygien_ovrigt: "Övrigt",
};

// ---------------------------------------------------------------------------
// Versionsmärkning för AI-kontrakt
// ---------------------------------------------------------------------------

export const AI_CONTRACT_VERSION = "1.0.0";
export const API_VERSION = "v1";
