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

/**
 * Date-seeded variant seed. These passives recur (weekly / nightly), so seeding
 * on the in-game day keeps copy stable within a firing but varied across the
 * many firings over time.
 */
function variantSeed(player: EventPlayerContext, salt: string): string {
  const day = (player as { dayOfYear?: unknown }).dayOfYear ?? 0;
  return `${salt}:${String(day)}`;
}

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

const checkinPrompts = [
  'A routine family check-in happened this week.',
  'You touched base with family this week - a call, a text thread, the usual.',
  'The weekly family catch-up came around again.',
  'You spent a little time staying in the family loop this week.',
];

const checkinResolutions = [
  'You stayed connected with family and strengthened support ties.',
  'The check-in reminded you that you have people in your corner.',
  'A small but real reinforcement of the family bonds you rely on.',
];

const mealPrompts = [
  'You had a quiet shared meal with family.',
  'Dinner with family tonight - nothing fancy, just together.',
  'You sat down to a simple meal with the family this evening.',
  'A shared dinner gave the evening a familiar, steady rhythm.',
];

const mealResolutions = [
  'The shared meal gave you a stable reset for the evening.',
  'A good meal and easy company left you settled and content.',
  'The quiet evening together recharged you for the days ahead.',
];

export const familyCatalog: EventDefinition[] = [
  {
    id: 'family_weekly_checkin',
    category: 'family',
    kind: 'passive',
    prompt: checkinPrompts[0],
    promptFn: seeded('checkin:prompt', checkinPrompts),
    isEligible: (player) => {
      const dayOfWeek = player.dayOfWeek as number | undefined;
      return dayOfWeek === 1;
    },
    choices: [
      {
        choiceId: 'checkin_complete',
        text: 'Check-in complete',
        resolutionText: checkinResolutions[0],
        resolutionTextFn: seeded('checkin:res', checkinResolutions),
        effects: {
          stats: {
            social: 6,
            happiness: 4,
          },
        },
      },
    ],
  },
  {
    id: 'family_shared_meal',
    category: 'family',
    kind: 'passive',
    prompt: mealPrompts[0],
    promptFn: seeded('meal:prompt', mealPrompts),
    isEligible: (player) => {
      const hour = player.hourOfDay as number | undefined;
      return hour === 19;
    },
    choices: [
      {
        choiceId: 'meal_done',
        text: 'Meal complete',
        resolutionText: mealResolutions[0],
        resolutionTextFn: seeded('meal:res', mealResolutions),
        effects: {
          stats: {
            happiness: 5,
          },
        },
      },
    ],
  },
];
