import type {
  Cuisine,
  MealType,
  NutritionValues,
  RecipeTag,
  Season,
  WeatherHint,
} from "@app/shared-types";
import type { CoverageResult } from "@app/recipe-engine";

/** Kandidat som poängsätts. Säkerhetsfiltrering har redan skett (blockers borta). */
export interface RecommendationCandidate {
  recipeId: string;
  titleSv: string;
  cuisine: Cuisine;
  tags: RecipeTag[];
  totalTimeMinutes: number;
  nutritionPerPortion: NutritionValues;
  estimatedCostMinorPerPortion?: number | null;
  ratingAverage?: number | null;
  ratingCount: number;
  peakSeasons: Season[];
  holidayTags: string[];
  spiceLevel: number;
  coverage: CoverageResult;
  /** Dagar sedan hushållet senast lagade receptet (null = aldrig). */
  daysSinceLastCooked?: number | null;
  /** Hushållets snittbetyg på receptet, om finns. */
  householdRating?: number | null;
}

export interface RecommendationContext {
  mealType: MealType;
  persons: number;
  maxMinutes?: number | undefined;
  maxCostMinorPerPortion?: number | undefined;
  /** Proteinlucka kvar i dag (g) för den som frågar – styr proteinfit. */
  remainingProteinG?: number | undefined;
  /** Kalorier kvar i dag – recept långt över straffas mjukt. */
  remainingKcal?: number | undefined;
  currentSeason: Season;
  activeHolidayTags: string[];
  weather?: WeatherHint | undefined;
  isWeekday: boolean;
  isTrainingDay?: boolean | undefined;
  favoriteCuisines: Cuisine[];
  /** Tolkade "jag är sugen på"-taggar (spec §19), redan normaliserade. */
  cravingTags?: string[] | undefined;
  cravingCuisine?: Cuisine | undefined;
  cravingMaxKcal?: number | undefined;
}

export interface ScoredRecommendation {
  recipeId: string;
  titleSv: string;
  score: number;
  /** Delpoäng för transparens och evals. */
  parts: Record<string, number>;
  whySv: string;
  missingIngredients: string[];
  usesExpiring: Array<{ nameSv: string; daysLeft: number | null }>;
  coveragePercent: number;
}

/** Vikter – justerbara via feature flags/admin utan koddeploy. */
export interface ScoringWeights {
  coverage: number;
  expiry: number;
  nutritionFit: number;
  taste: number;
  rating: number;
  season: number;
  holiday: number;
  time: number;
  budget: number;
  variety: number;
  weather: number;
  craving: number;
}

export const DEFAULT_WEIGHTS: ScoringWeights = {
  coverage: 30,
  expiry: 20,
  nutritionFit: 12,
  taste: 10,
  rating: 8,
  season: 4,
  holiday: 6,
  time: 8,
  budget: 6,
  variety: 6,
  weather: 3,
  craving: 15,
};
