import type { DynamicTextFn, EventDefinition, EventPlayerContext } from '../types.js';
import { pickVariant } from '../../../utils/textVariations.js';

/**
 * Date-seeded variant seed. Holidays fire once per calendar year, so seeding on
 * the player's age/day keeps the copy STABLE within a single year's firing but
 * VARIED across years (the player sees a different New Year line each year).
 */
function variantSeed(player: EventPlayerContext, salt: string): string {
  const day =
    (player as { dayOfYear?: unknown }).dayOfYear ??
    (player as { date?: unknown }).date ??
    0;
  return `${salt}:${player.c.ageYears ?? 0}:${String(day)}`;
}

function seeded(salt: string, variants: string[]): DynamicTextFn {
  return (player) => pickVariant(variants, variantSeed(player, salt));
}

function passiveHoliday(
  id: string,
  prompts: string[],
  date: string,
  resolutions: string[],
  happinessDelta: number
): EventDefinition {
  return {
    id,
    category: 'holidays',
    kind: 'passive',
    prompt: prompts[0],
    promptFn: seeded(`${id}:prompt`, prompts),
    isEligible: (player) => (player.date as string | undefined) === date,
    choices: [
      {
        choiceId: 'acknowledge',
        text: 'Acknowledge',
        resolutionText: resolutions[0],
        resolutionTextFn: seeded(`${id}:res`, resolutions),
        effects: {
          stats: {
            happiness: happinessDelta,
          },
        },
      },
    ],
  };
}

export const holidayCatalog: EventDefinition[] = [
  passiveHoliday(
    'holiday_new_year',
    [
      'A new year begins with fresh momentum.',
      'The calendar turns over and a new year stretches out ahead of you.',
      'Midnight came and went - it is a brand new year, full of possibility.',
      'A fresh start. The new year invites you to reset and aim higher.',
    ],
    '01-01',
    [
      'You welcomed the new year with renewed focus.',
      'You set your intentions for the year and felt a quiet optimism.',
      'You toasted to new beginnings and let last year go.',
    ],
    10
  ),
  passiveHoliday(
    'holiday_valentines',
    [
      "It's Valentine's Day and relationships feel more visible today.",
      "Valentine's Day is here - love is in the air, for better or worse.",
      "The shops are full of hearts and flowers. It's Valentine's Day.",
      "Valentine's Day arrives, nudging you to think about who matters to you.",
    ],
    '02-14',
    [
      'You reflected on connection and what you value in relationships.',
      'You spent the day appreciating the people who matter to you.',
      'You leaned into the spirit of the day and felt a little warmer for it.',
    ],
    6
  ),
  passiveHoliday(
    'holiday_halloween',
    [
      'Halloween energy is everywhere tonight.',
      'Costumes, candy, and carved pumpkins - it is Halloween.',
      'The neighborhood is decked out in cobwebs and jack-o-lanterns tonight.',
      'A spooky chill is in the air. Halloween has arrived.',
    ],
    '10-31',
    [
      'You embraced the fun and enjoyed the seasonal atmosphere.',
      'You leaned into the spooky spirit and had a genuinely good time.',
      'You handed out candy (and snuck a few pieces) and enjoyed the night.',
    ],
    4
  ),
  passiveHoliday(
    'holiday_christmas',
    [
      'Christmas arrived with family expectations and reflection.',
      'The lights are up and Christmas morning is finally here.',
      "It's Christmas - a day of warmth, family, and a little chaos.",
      'Christmas brings its usual mix of joy, nostalgia, and obligation.',
    ],
    '12-25',
    [
      'You closed out the holiday with gratitude and perspective.',
      'You soaked in the warmth of the season and felt grateful.',
      'You wrapped up the holiday feeling connected and content.',
    ],
    8
  ),
];
