/**
 * Centrala enums för hela plattformen.
 *
 * Mönster: `as const`-array + härledd unionstyp. Arrayerna återanvänds av
 * Zod (`z.enum`) och Drizzle (`pgEnum`) så att databas, API och mobil alltid
 * delar exakt samma värdemängder.
 */

// ---------------------------------------------------------------------------
// Förvaring & lager (spec §8)
// ---------------------------------------------------------------------------

export const STORAGE_LOCATION_TYPES = [
  "fridge",
  "freezer",
  "pantry",
  "garage_freezer",
  "wine_fridge",
  "cellar",
  "meal_box",
  "custom",
] as const;
export type StorageLocationType = (typeof STORAGE_LOCATION_TYPES)[number];

/** Datakällor för lagerposter (spec §9). */
export const INVENTORY_SOURCES = [
  "fridge_photo",
  "freezer_photo",
  "pantry_photo",
  "ingredient_photo",
  "barcode",
  "receipt",
  "digital_receipt",
  "label_photo",
  "manual_search",
  "free_text",
  "voice",
  "cooked_recipe",
  "connector",
  "seed",
] as const;
export type InventorySource = (typeof INVENTORY_SOURCES)[number];

/** Lagret är transaktionsbaserat (spec §8): varje förändring är en transaktion. */
export const INVENTORY_TRANSACTION_TYPES = [
  "purchase",
  "consume",
  "discard",
  "adjust",
  "cook_use",
  "leftover_created",
  "leftover_consumed",
  "correction",
  "freeze",
  "thaw",
  "move",
] as const;
export type InventoryTransactionType = (typeof INVENTORY_TRANSACTION_TYPES)[number];

/** Klassning av hur bråttom en vara är (deterministisk, spec §13). */
export const EXPIRY_STATUSES = ["fresh", "use_soon", "expiring", "expired", "unknown"] as const;
export type ExpiryStatus = (typeof EXPIRY_STATUSES)[number];

/** Bäst före ≠ sista förbrukningsdag (spec §13). */
export const DATE_KINDS = ["best_before", "use_by"] as const;
export type DateKind = (typeof DATE_KINDS)[number];

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

/**
 * Språkneutrala enhetskoder (i18n-spec §11). Canonical bas: massa=GRAM,
 * volym=MILLILITER, antal=COUNT. Visningsnamn/förkortningar ("msk", "tbsp")
 * lever i översättningslagret – ALDRIG här. Dokumenterad standard (i18n-spec §10):
 * TEASPOON=5 ml, TABLESPOON=15 ml (metrisk), CUP_US=236.59 ml,
 * FLUID_OUNCE_US=29.57 ml, OUNCE=28.35 g, POUND=453.59 g, PINCH≈0.5 ml.
 * Svenska "krm" (=1 ml) lagras som MILLILITER och visas lokalt som "krm".
 */
export const UNITS = [
  "GRAM",
  "KILOGRAM",
  "MILLILITER",
  "DECILITER",
  "LITER",
  "TEASPOON",
  "TABLESPOON",
  "CUP_US",
  "FLUID_OUNCE_US",
  "OUNCE",
  "POUND",
  "COUNT",
  "PORTION",
  "PINCH",
  "SLICE",
  "CLOVE",
  "CAN",
  "PACKAGE",
] as const;
export type Unit = (typeof UNITS)[number];

export const UNIT_KINDS = ["mass", "volume", "count"] as const;
export type UnitKind = (typeof UNIT_KINDS)[number];

// ---------------------------------------------------------------------------
// Användare, profil & mål (spec §6)
// ---------------------------------------------------------------------------

export const USER_ROLES = ["user", "moderator", "admin"] as const;
export type UserRole = (typeof USER_ROLES)[number];

export const SEXES = ["female", "male", "unspecified"] as const;
export type Sex = (typeof SEXES)[number];

export const ACTIVITY_LEVELS = ["sedentary", "light", "moderate", "active", "very_active"] as const;
export type ActivityLevel = (typeof ACTIVITY_LEVELS)[number];

export const GOAL_TYPES = [
  "lose_weight",
  "gain_weight",
  "build_muscle",
  "maintain_weight",
  "more_protein",
  "less_fat",
  "more_fiber",
  "more_variety",
  "less_waste",
  "lower_cost",
  "cook_more",
] as const;
export type GoalType = (typeof GOAL_TYPES)[number];

export const DIET_PATTERNS = [
  "omnivore",
  "flexitarian",
  "pescatarian",
  "vegetarian",
  "vegan",
  "low_carb",
  "keto",
  "high_protein",
  "mediterranean",
] as const;
export type DietPattern = (typeof DIET_PATTERNS)[number];

export const RELIGIOUS_RULES = [
  "none",
  "halal",
  "kosher",
  "hindu_no_beef",
  "buddhist_vegetarian",
] as const;
export type ReligiousRule = (typeof RELIGIOUS_RULES)[number];

/** EU:s 14 deklarationspliktiga allergener. Allergikontroll är ALLTID deterministisk (spec §61.2). */
export const ALLERGENS = [
  "gluten",
  "crustaceans",
  "eggs",
  "fish",
  "peanuts",
  "soy",
  "milk",
  "tree_nuts",
  "celery",
  "mustard",
  "sesame",
  "sulphites",
  "lupin",
  "molluscs",
] as const;
export type Allergen = (typeof ALLERGENS)[number];

/** Enkelt läge vs exakt läge (spec §5) – kan kombineras, lagras som preferens. */
export const PRECISION_MODES = ["simple", "exact"] as const;
export type PrecisionMode = (typeof PRECISION_MODES)[number];

// ---------------------------------------------------------------------------
// Hushåll (spec §7)
// ---------------------------------------------------------------------------

export const HOUSEHOLD_ROLES = ["owner", "adult", "member", "child"] as const;
export type HouseholdRole = (typeof HOUSEHOLD_ROLES)[number];

// ---------------------------------------------------------------------------
// Recept (spec §14–16)
// ---------------------------------------------------------------------------

export const CUISINES = [
  "swedish",
  "nordic",
  "italian",
  "french",
  "spanish",
  "greek",
  "thai",
  "chinese",
  "japanese",
  "korean",
  "vietnamese",
  "indian",
  "mexican",
  "american",
  "turkish",
  "lebanese",
  "moroccan",
  "middle_eastern",
  "international",
] as const;
export type Cuisine = (typeof CUISINES)[number];

export const MEAL_TYPES = [
  "breakfast",
  "lunch",
  "dinner",
  "snack",
  "dessert",
  "starter",
  "buffet",
  "party",
] as const;
export type MealType = (typeof MEAL_TYPES)[number];

/** Attribut-taggar från spec §14 (utöver måltidstyp och metod). */
export const RECIPE_TAGS = [
  "kid_friendly",
  "meal_prep",
  "quick",
  "high_protein",
  "low_fat",
  "low_calorie",
  "low_carb",
  "vegetarian",
  "vegan",
  "gluten_free",
  "lactose_free",
  "budget",
  "luxury",
  "freezer_friendly",
  "leftover_friendly",
  "one_pot",
  "batch_cooking",
] as const;
export type RecipeTag = (typeof RECIPE_TAGS)[number];

export const COOKING_METHODS = [
  "stovetop",
  "oven",
  "grill",
  "airfryer",
  "wok",
  "slow_cooker",
  "sous_vide",
  "microwave",
  "no_cook",
  "pressure_cooker",
  "deep_fry",
  "steam",
] as const;
export type CookingMethod = (typeof COOKING_METHODS)[number];

export const EQUIPMENT = [
  "stove",
  "oven",
  "microwave",
  "airfryer",
  "grill",
  "slow_cooker",
  "sous_vide",
  "pressure_cooker",
  "blender",
  "food_processor",
  "hand_mixer",
  "stand_mixer",
  "wok_pan",
  "kitchen_scale",
  "thermometer",
] as const;
export type Equipment = (typeof EQUIPMENT)[number];

export const RECIPE_DIFFICULTIES = ["beginner", "easy", "medium", "advanced", "expert"] as const;
export type RecipeDifficulty = (typeof RECIPE_DIFFICULTIES)[number];

/** Publiceringsflöde för recept (spec §35): submission → AI-kontroll → dubblett → moderation → publicering. */
export const RECIPE_STATUSES = [
  "draft",
  "submitted",
  "ai_checked",
  "in_moderation",
  "published",
  "rejected",
  "archived",
] as const;
export type RecipeStatus = (typeof RECIPE_STATUSES)[number];

export const RECIPE_VERIFICATION_STATUSES = [
  "unverified",
  "community",
  "verified",
  "editorial",
] as const;
export type RecipeVerificationStatus = (typeof RECIPE_VERIFICATION_STATUSES)[number];

/** Varianter länkas till grundrecept (spec §16). */
export const RECIPE_VARIANT_TYPES = [
  "standard",
  "high_protein",
  "low_calorie",
  "low_fat",
  "vegetarian",
  "vegan",
  "gluten_free",
  "lactose_free",
  "kid_friendly",
  "airfryer",
  "budget",
] as const;
export type RecipeVariantType = (typeof RECIPE_VARIANT_TYPES)[number];

/** Dubblettklassning (spec §36). */
export const RECIPE_SIMILARITY_CLASSES = [
  "duplicate",
  "variant",
  "inspired",
  "independent",
] as const;
export type RecipeSimilarityClass = (typeof RECIPE_SIMILARITY_CLASSES)[number];

/** Juridiskt spårbara källtyper för recept (spec §15). */
export const RECIPE_SOURCE_TYPES = [
  "own_editorial",
  "ai_assisted_reviewed",
  "open_license",
  "public_domain",
  "licensed_database",
  "creator_agreement",
  "user_generated",
] as const;
export type RecipeSourceType = (typeof RECIPE_SOURCE_TYPES)[number];

// ---------------------------------------------------------------------------
// Skanning & AI-jobb (spec §4.2, §54)
// ---------------------------------------------------------------------------

export const SCAN_TYPES = [
  "fridge",
  "freezer",
  "pantry",
  "ingredients",
  "plate",
  "receipt",
  "barcode",
  "expiry_date",
  "nutrition_label",
  "product_package",
] as const;
export type ScanType = (typeof SCAN_TYPES)[number];

/** Jobbtyper, exakt enligt spec §54. */
export const JOB_TYPES = [
  "ANALYZE_FRIDGE_IMAGE",
  "ANALYZE_PANTRY_IMAGE",
  "ANALYZE_MEAL_IMAGE",
  "READ_RECEIPT",
  "READ_NUTRITION_LABEL",
  "READ_EXPIRY_DATE",
  "NORMALIZE_PRODUCTS",
  "DEDUPLICATE_INVENTORY",
  "CALCULATE_NUTRITION",
  "GENERATE_RECIPE_OPTIONS",
  "RANK_RECIPES",
  "UPDATE_USER_MEMORY",
  "GENERATE_WEEK_PLAN",
  "SEND_EXPIRY_NOTIFICATION",
  "VERIFY_SUBSCRIPTION",
  "PROCESS_STORE_NOTIFICATION",
  "BUILD_TRAINING_SAMPLE",
  "RUN_AI_EVALUATION",
] as const;
export type JobType = (typeof JOB_TYPES)[number];

export const JOB_STATUSES = [
  "queued",
  "running",
  "awaiting_confirmation",
  "completed",
  "failed",
  "canceled",
] as const;
export type JobStatus = (typeof JOB_STATUSES)[number];

// ---------------------------------------------------------------------------
// Events (spec §55)
// ---------------------------------------------------------------------------

export const EVENT_TYPES = [
  "PRODUCT_ADDED",
  "PRODUCT_UPDATED",
  "PRODUCT_CONSUMED",
  "PRODUCT_DISCARDED",
  "RECIPE_COOKED",
  "RECIPE_RATED",
  "RECIPE_CREATED",
  "RECIPE_FORKED",
  "MEAL_LOGGED",
  "MEAL_PHOTO_ANALYZED",
  "HOUSEHOLD_MEMBER_ADDED",
  "SUBSCRIPTION_STARTED",
  "SUBSCRIPTION_CHANGED",
  "AI_CORRECTED",
  "MEMORY_UPDATED",
  "SHOPPING_COMPLETED",
  "WEEK_PLAN_UPDATED",
  "MEAL_BOX_CREATED",
  "MEAL_BOX_CONSUMED",
] as const;
export type EventType = (typeof EVENT_TYPES)[number];

// ---------------------------------------------------------------------------
// Måltider & loggning (spec §23)
// ---------------------------------------------------------------------------

export const MEAL_LOG_SOURCES = [
  "cooked_recipe",
  "plate_photo",
  "barcode",
  "product",
  "free_text",
  "voice",
  "previous_meal",
  "meal_box",
  "manual",
] as const;
export type MealLogSource = (typeof MEAL_LOG_SOURCES)[number];

// ---------------------------------------------------------------------------
// Smakprofil & feedback (spec §30)
// ---------------------------------------------------------------------------

export const FEEDBACK_TAGS = [
  "too_spicy",
  "too_mild",
  "too_dry",
  "too_salty",
  "too_sour",
  "too_little_sauce",
  "too_difficult",
  "make_again",
] as const;
export type FeedbackTag = (typeof FEEDBACK_TAGS)[number];

export const TASTE_AXES = [
  "spice",
  "salt",
  "acid",
  "creaminess",
  "garlic",
  "sweetness",
  "herbs",
  "umami",
] as const;
export type TasteAxis = (typeof TASTE_AXES)[number];

/** Skilj explicit preferens, observerat mönster och AI-antagande (spec §30, §32). */
export const SIGNAL_ORIGINS = ["user_stated", "observed", "ai_inferred"] as const;
export type SignalOrigin = (typeof SIGNAL_ORIGINS)[number];

// ---------------------------------------------------------------------------
// Minne & samtycke (spec §32–33)
// ---------------------------------------------------------------------------

export const MEMORY_KINDS = [
  "structured_fact",
  "event",
  "semantic",
  "profile_summary",
  "recipe_memory",
] as const;
export type MemoryKind = (typeof MEMORY_KINDS)[number];

export const CONSENT_KINDS = [
  "personalization",
  "anonymized_improvement",
  "image_training",
  "health_integration",
  "location_weather",
  "push_notifications",
  /** Product analytics – legitimate interest with opt-out (spec §8, §33). */
  "product_analytics",
] as const;
export type ConsentKind = (typeof CONSENT_KINDS)[number];

export const CONSENT_STATUSES = ["granted", "denied", "revoked"] as const;
export type ConsentStatus = (typeof CONSENT_STATUSES)[number];

// ---------------------------------------------------------------------------
// Säsong, högtid, väder (spec §28–29)
// ---------------------------------------------------------------------------

export const SEASONS = ["spring", "summer", "autumn", "winter"] as const;
export type Season = (typeof SEASONS)[number];

export const WEATHER_HINTS = ["hot", "warm", "mild", "cold", "rain", "snow", "unknown"] as const;
export type WeatherHint = (typeof WEATHER_HINTS)[number];

// ---------------------------------------------------------------------------
// Prenumerationer (spec §45–47)
// ---------------------------------------------------------------------------

export const SUBSCRIPTION_PLANS = ["free", "household", "family", "large_household"] as const;
export type SubscriptionPlan = (typeof SUBSCRIPTION_PLANS)[number];

export const SUBSCRIPTION_STATUSES = [
  "trial",
  "active",
  "in_grace",
  "on_hold",
  "paused",
  "canceled",
  "expired",
] as const;
export type SubscriptionStatus = (typeof SUBSCRIPTION_STATUSES)[number];

export const SUBSCRIPTION_PROVIDERS = ["apple", "google", "promo", "none"] as const;
export type SubscriptionProvider = (typeof SUBSCRIPTION_PROVIDERS)[number];

// ---------------------------------------------------------------------------
// Creator & community (spec §35–38)
// ---------------------------------------------------------------------------

export const CREATOR_LEVELS = ["beginner", "sous_chef", "chef", "master_chef", "legend"] as const;
export type CreatorLevel = (typeof CREATOR_LEVELS)[number];

export const MODERATION_ACTIONS = ["approve", "reject", "request_changes", "escalate"] as const;
export type ModerationAction = (typeof MODERATION_ACTIONS)[number];

export const PROFILE_VISIBILITIES = ["private", "friends", "national", "global"] as const;
export type ProfileVisibility = (typeof PROFILE_VISIBILITIES)[number];

// ---------------------------------------------------------------------------
// Notiser (spec §40)
// ---------------------------------------------------------------------------

export const NOTIFICATION_TYPES = [
  "expiry_warning",
  "meal_box_reminder",
  "quick_dinner_suggestion",
  "holiday_upcoming",
  "creator_new_recipe",
  "week_plan_change",
  "pantry_forecast",
  "subscription_status",
] as const;
export type NotificationType = (typeof NOTIFICATION_TYPES)[number];

// ---------------------------------------------------------------------------
// Datakvalitet – varje AI-datapunkt bär källa + confidence (spec §9)
// ---------------------------------------------------------------------------

export const VERIFICATION_STATUSES = [
  "unverified",
  "user_verified",
  "editorially_verified",
] as const;
export type VerificationStatus = (typeof VERIFICATION_STATUSES)[number];

// ---------------------------------------------------------------------------
// Översättningar (i18n-spec §11–14): AI-utkast -> granskning -> publicerad.
// Svensk källtext är alltid sanningen; översättningar är vyer av den.
// ---------------------------------------------------------------------------

export const TRANSLATION_STATUSES = ["draft_ai", "in_review", "published"] as const;
export type TranslationStatus = (typeof TRANSLATION_STATUSES)[number];

export const TRANSLATION_SOURCES = ["seed", "ai", "human"] as const;
export type TranslationSource = (typeof TRANSLATION_SOURCES)[number];

/**
 * Hjälpfunktion för Drizzle pgEnum som kräver en icke-tom tuple.
 * Användning: pgEnum("unit", tuple(UNITS))
 */
export function tuple<T extends readonly [string, ...string[]]>(
  values: T,
): [T[number], ...T[number][]] {
  return [...values] as [T[number], ...T[number][]];
}
