/**
 * Hobbies & Personal Development Activity Events
 * Personal growth, hobbies, and self-improvement activities
 *
 * Events:
 * - GardeningEvent: Starting a garden (ages 20-100) - Uses ActivityRecord to track plants grown
 * - MeditationEvent: Beginning a meditation practice (ages 14-100) - Uses Habit system for daily practice
 * - BirdWatchingEvent: Taking up birdwatching (ages 30-100) - Uses ActivityRecord to track species spotted
 * - CollectionHobbyEvent: Starting a collection hobby (ages 8-100) - Uses ActivityRecord to track collection
 * - ReadingChallengeEvent: Challenging yourself to read more books (ages 10-100) - Uses ActivityRecord to track books read
 * - FishingHobbyEvent: Taking up fishing (ages 10-100)
 * - PuzzleHobbyEvent: Puzzle and board game hobbies (ages 8-100)
 * - AstronomyHobbyEvent: Stargazing and astronomy (ages 10-100)
 * - ContentCreationEvent: Starting a podcast/YouTube/blog (ages 14-65)
 * - CookingHobbyEvent: Learning to cook specific cuisines (ages 14-100)
 */

import { BaseEvent, EventResult, AnswerOption, EventConfig } from '../base.js';
import { Player } from '../../models/index.js';

/**
 * Helper to generate unique activity ID
 */
function generateActivityId(prefix: string): string {
  return `${prefix}_${Math.random().toString(36).substring(2, 10)}`;
}

/**
 * Helper to add activity and activity record to player
 */
function addActivityToPlayer(
  player: Player,
  activityId: string,
  title: string,
  type: string,
  description: string,
  focus: string,
  initialPerformance: number = 50
): void {
  const c = player.c;

  // Add activity
  if (!c.activities) c.activities = [];
  c.activities.push({
    id: activityId,
    title,
    type,
  });

  // Add activity record
  if (!c.activityRecords) c.activityRecords = [];
  c.activityRecords.push({
    id: activityId,
    type,
    dateStarted: player.date,
    performance: initialPerformance,
    focus,
    achievements: [],
    level: 1,
  });
}

/**
 * Helper to add a schedule to player
 */
function addScheduleToPlayer(
  player: Player,
  title: string,
  frequency: string[],
  duration: number,
  location?: string
): void {
  const c = player.c;
  if (!c.schedules) c.schedules = [];

  c.schedules.push({
    id: generateActivityId('schedule'),
    title,
    location: location ?? `home${c.id}`,
    duration,
    days: {
      daysOfWeek: frequency,
    },
  });
}

/**
 * Helper to add a habit to player
 */
function addHabitToPlayer(player: Player, habitName: string): void {
  const c = player.c;
  if (!c.habits) c.habits = [];

  // Check if habit already exists
  if (!c.habits.some((h) => h.name === habitName)) {
    c.habits.push({
      name: habitName,
      status: 'active',
    });
  }
}

/**
 * Helper to add a one-time event to player
 */
function addOneTimeEventToPlayer(
  player: Player,
  title: string,
  message: string,
  daysFromNow: number
): void {
  const c = player.c;
  if (!c.oneTimeEvents) c.oneTimeEvents = [];

  // Calculate future date
  const futureDate = new Date(player.date);
  futureDate.setDate(futureDate.getDate() + daysFromNow);

  c.oneTimeEvents.push({
    id: generateActivityId('event'),
    date: futureDate.toISOString().split('T')[0],
    hour: 10,
    message: `${title}: ${message}`,
  });
}

/**
 * Gardening Event - Enhanced with activity records and follow-up events
 */
export class GardeningEvent extends BaseEvent {
  readonly id = 'gardening';

  getConfig(): EventConfig {
    return {
      minAge: 20,
      maxAge: 100,
      baseChance: 0.0002,
      triggerType: 'random',
    };
  }

  checkConditions(player: Player): boolean {
    const c = player.c;
    return (
      !player.askedQuestions.has(this.id) && c.ageYears >= 20 && c.ageYears <= 100
    );
  }

  getQuestion(): string {
    return 'You want to start a garden. Begin?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, full vegetable garden!', data: 'vegetable', energyCost: 15, moneyCost: 100 },
      { option: 'Small herb garden', data: 'herb', moneyCost: 30 },
      { option: 'Just houseplants', data: 'houseplants', moneyCost: 20 },
      { option: 'Not interested', data: 'no' },
    ];
  }

  processAnswer(player: Player, selectedOption: number): EventResult {
    const c = player.c;

    if (selectedOption === 0) {
      if ((c.money ?? 0) < 100) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for a vegetable garden right now.",
        };
      }
      c.happiness = Math.min(100, (c.happiness ?? 50) + 25);
      c.energy = Math.max(0, c.energy - 15);
      c.money = (c.money ?? 0) - 100;
      c.stress = Math.max(0, (c.stress ?? 0) - 15);

      // Create gardening activity with ActivityRecord
      const gardenId = generateActivityId('gardening');
      addActivityToPlayer(
        player,
        gardenId,
        'Vegetable Garden',
        'hobby',
        'Growing vegetables at home',
        'vegetable_garden',
        50
      );

      // Add gardening schedule (2x per week)
      addScheduleToPlayer(player, 'Gardening', ['tuesday', 'saturday'], 15);

      // Add follow-up event for first harvest
      addOneTimeEventToPlayer(
        player,
        'First Harvest',
        "Your vegetable garden is thriving! You harvest your first homegrown tomatoes, lettuce, and herbs. Nothing tastes quite like vegetables you grew yourself!",
        60
      );

      return {
        type: 'messageEvent',
        message:
          "You've started a full vegetable garden! It will require regular care, but the fresh produce will be worth it.",
      };
    } else if (selectedOption === 1) {
      if ((c.money ?? 0) < 30) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for an herb garden right now.",
        };
      }
      c.happiness = Math.min(100, (c.happiness ?? 50) + 15);
      c.money = (c.money ?? 0) - 30;
      c.stress = Math.max(0, (c.stress ?? 0) - 10);

      // Create herb garden activity
      const herbId = generateActivityId('herb_garden');
      addActivityToPlayer(
        player,
        herbId,
        'Herb Garden',
        'hobby',
        'Growing herbs on windowsill',
        'herb_garden',
        60
      );

      return {
        type: 'messageEvent',
        message:
          "You've planted a small herb garden on your windowsill. Fresh basil and rosemary at your fingertips!",
      };
    } else if (selectedOption === 2) {
      if ((c.money ?? 0) < 20) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for houseplants right now.",
        };
      }
      c.happiness = Math.min(100, (c.happiness ?? 50) + 10);
      c.money = (c.money ?? 0) - 20;
      c.stress = Math.max(0, (c.stress ?? 0) - 5);

      return {
        type: 'messageEvent',
        message:
          "You've brought home some beautiful houseplants. They brighten up your living space nicely.",
      };
    } else {
      c.happiness = Math.max(0, (c.happiness ?? 50) - 5);
      return {
        type: 'messageEvent',
        message: "You decide gardening isn't for you right now.",
      };
    }
  }
}

/**
 * Meditation Event - Enhanced with habit system integration
 */
export class MeditationEvent extends BaseEvent {
  readonly id = 'meditation';

  getConfig(): EventConfig {
    return {
      minAge: 14,
      maxAge: 100,
      baseChance: 0.0002,
      triggerType: 'random',
    };
  }

  checkConditions(player: Player): boolean {
    const c = player.c;
    return (
      !player.askedQuestions.has(this.id) && c.ageYears >= 14 && c.ageYears <= 100
    );
  }

  getQuestion(): string {
    return 'You want to start meditating daily. Begin a practice?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, meditate daily!', data: 'daily' },
      { option: 'Try occasionally', data: 'occasional' },
      { option: 'Use meditation app', data: 'app', moneyCost: 10 },
      { option: 'Not for me', data: 'no' },
    ];
  }

  processAnswer(player: Player, selectedOption: number): EventResult {
    const c = player.c;

    if (selectedOption === 0) {
      c.stress = Math.max(0, (c.stress ?? 0) - 30);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 20);
      c.health = Math.min(100, (c.health ?? 50) + 5);

      // Add meditation as a positive habit
      addHabitToPlayer(player, 'meditation');

      // Create daily meditation schedule
      addScheduleToPlayer(player, 'Daily meditation', ['daily'], 10);

      // Add follow-up event for meditation milestone
      addOneTimeEventToPlayer(
        player,
        'Meditation Milestone',
        "You've been meditating consistently for 30 days! You notice you're calmer, more focused, and better at managing stress. The practice has become an essential part of your routine.",
        30
      );

      return {
        type: 'messageEvent',
        message:
          "You've committed to daily meditation. The practice brings you peace and clarity each morning.",
      };
    } else if (selectedOption === 1) {
      c.stress = Math.max(0, (c.stress ?? 0) - 15);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 10);

      return {
        type: 'messageEvent',
        message:
          'You try meditating when you remember. Even occasional practice helps calm your mind.',
      };
    } else if (selectedOption === 2) {
      if ((c.money ?? 0) < 10) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for the meditation app subscription.",
        };
      }
      c.money = (c.money ?? 0) - 10;
      c.stress = Math.max(0, (c.stress ?? 0) - 25);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 15);
      c.health = Math.min(100, (c.health ?? 50) + 5);

      // Add meditation app habit
      addHabitToPlayer(player, 'meditation_app');

      // Create daily guided meditation schedule
      addScheduleToPlayer(player, 'Guided meditation', ['daily'], 15);

      return {
        type: 'messageEvent',
        message:
          "You've subscribed to a meditation app. The guided sessions make it easy to build a consistent practice.",
      };
    } else {
      c.happiness = Math.max(0, (c.happiness ?? 50) - 5);
      return {
        type: 'messageEvent',
        message: "You decide meditation isn't your thing.",
      };
    }
  }
}

/**
 * Bird Watching Event - Enhanced with species tracking
 */
export class BirdWatchingEvent extends BaseEvent {
  readonly id = 'birdWatching';

  getConfig(): EventConfig {
    return {
      minAge: 30,
      maxAge: 100,
      baseChance: 0.0002,
      triggerType: 'random',
    };
  }

  checkConditions(player: Player): boolean {
    const c = player.c;
    return (
      !player.askedQuestions.has(this.id) && c.ageYears >= 30 && c.ageYears <= 100
    );
  }

  getQuestion(): string {
    return "You're interested in birdwatching. Start?";
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, buy binoculars!', data: 'binoculars', moneyCost: 100 },
      { option: 'Use what I have', data: 'basic' },
      { option: 'Join birdwatching group', data: 'group', moneyCost: 50 },
      { option: 'Not interested', data: 'no' },
    ];
  }

  processAnswer(player: Player, selectedOption: number): EventResult {
    const c = player.c;

    if (selectedOption === 0) {
      if ((c.money ?? 0) < 100) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for binoculars right now.",
        };
      }
      c.money = (c.money ?? 0) - 100;
      c.happiness = Math.min(100, (c.happiness ?? 50) + 20);
      c.stress = Math.max(0, (c.stress ?? 0) - 15);

      // Create birdwatching activity with ActivityRecord
      const birdingId = generateActivityId('birdwatching');
      addActivityToPlayer(
        player,
        birdingId,
        'Birdwatching',
        'hobby',
        'Observing and identifying bird species',
        'serious_birder',
        10 // Species spotted counter starts at 10
      );

      // Create weekend birdwatching schedule
      addScheduleToPlayer(player, 'Birdwatching', ['saturday', 'sunday'], 40, `park${c.id}`);

      // Add follow-up event for first rare sighting
      addOneTimeEventToPlayer(
        player,
        'Rare Bird Sighting',
        'You spotted a rare bird species through your binoculars! You carefully note it in your bird journal. Your hobby is becoming quite rewarding!',
        45
      );

      return {
        type: 'messageEvent',
        message:
          "You've invested in quality binoculars! You can't wait to identify all the birds in your area.",
      };
    } else if (selectedOption === 1) {
      c.happiness = Math.min(100, (c.happiness ?? 50) + 15);
      c.stress = Math.max(0, (c.stress ?? 0) - 10);

      // Create basic birdwatching activity
      const birdingId = generateActivityId('birdwatching');
      addActivityToPlayer(
        player,
        birdingId,
        'Birdwatching',
        'hobby',
        'Casual bird observation',
        'casual_birder',
        5 // Fewer species spotted
      );

      return {
        type: 'messageEvent',
        message:
          "You start birdwatching with what you have. It's peaceful watching nature from your backyard.",
      };
    } else if (selectedOption === 2) {
      if ((c.money ?? 0) < 50) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for the club membership.",
        };
      }
      c.money = (c.money ?? 0) - 50;
      c.happiness = Math.min(100, (c.happiness ?? 50) + 25);
      c.social = Math.min(100, (c.social ?? 50) + 20);
      c.stress = Math.max(0, (c.stress ?? 0) - 15);

      // Create social birdwatching activity
      const birdingId = generateActivityId('birdwatching');
      addActivityToPlayer(
        player,
        birdingId,
        'Birdwatching Club',
        'hobby',
        'Birding with a local group',
        'social_birder',
        15 // Group helps you spot more species
      );

      // Create group birdwatching schedule
      addScheduleToPlayer(player, 'Birdwatching Club', ['saturday'], 60, `park${c.id}`);

      return {
        type: 'messageEvent',
        message:
          "You've joined a local birdwatching group! You make new friends who share your appreciation for nature.",
      };
    } else {
      c.happiness = Math.max(0, (c.happiness ?? 50) - 5);
      return {
        type: 'messageEvent',
        message: "You decide birdwatching isn't for you.",
      };
    }
  }
}

/**
 * Collection Hobby Event - Enhanced with age-appropriate collection types
 */
export class CollectionHobbyEvent extends BaseEvent {
  readonly id = 'collectionHobby';

  private getCollectionType(age: number): string {
    if (age < 12) {
      const types = ['rocks', 'stickers', 'trading cards'];
      return types[Math.floor(Math.random() * types.length)];
    } else if (age < 18) {
      const types = ['trading cards', 'vinyl records', 'vintage posters'];
      return types[Math.floor(Math.random() * types.length)];
    } else {
      const types = ['coins', 'stamps', 'vintage items', 'art prints', 'vinyl records'];
      return types[Math.floor(Math.random() * types.length)];
    }
  }

  getConfig(): EventConfig {
    return {
      minAge: 8,
      maxAge: 100,
      baseChance: 0.0002,
      triggerType: 'random',
    };
  }

  checkConditions(player: Player): boolean {
    const c = player.c;
    return (
      !player.askedQuestions.has(this.id) && c.ageYears >= 8 && c.ageYears <= 100
    );
  }

  getQuestion(player?: Player): string {
    const collectionType = player
      ? this.getCollectionType(player.c.ageYears)
      : 'collectibles';
    return `You want to start collecting ${collectionType}. Begin?`;
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, start collection!', data: 'serious', moneyCost: 50 },
      { option: 'Casual collecting', data: 'casual', moneyCost: 20 },
      { option: 'Join collector community', data: 'community', moneyCost: 75 },
      { option: 'Not interested', data: 'no' },
    ];
  }

  processAnswer(player: Player, selectedOption: number): EventResult {
    const c = player.c;
    const collectionType = this.getCollectionType(c.ageYears);

    if (selectedOption === 0) {
      if ((c.money ?? 0) < 50) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money to start a collection right now.",
        };
      }
      c.money = (c.money ?? 0) - 50;
      c.happiness = Math.min(100, (c.happiness ?? 50) + 20);
      c.prestige = (c.prestige ?? 0) + 5;

      // Create collection activity with ActivityRecord
      const collectionId = generateActivityId('collection');
      addActivityToPlayer(
        player,
        collectionId,
        `${collectionType.charAt(0).toUpperCase() + collectionType.slice(1)} Collection`,
        'hobby',
        `Collecting ${collectionType}`,
        `${collectionType}_serious`,
        10 // Items in collection
      );

      // Add follow-up event for rare find
      addOneTimeEventToPlayer(
        player,
        'Rare Collection Find',
        `You found an incredibly rare piece for your ${collectionType} collection! It's worth significantly more than what you paid. Your collection is really coming together!`,
        90
      );

      return {
        type: 'messageEvent',
        message: `You've started your ${collectionType} collection! You love the thrill of finding rare pieces.`,
      };
    } else if (selectedOption === 1) {
      if ((c.money ?? 0) < 20) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for collecting right now.",
        };
      }
      c.money = (c.money ?? 0) - 20;
      c.happiness = Math.min(100, (c.happiness ?? 50) + 15);

      // Create casual collection activity
      const collectionId = generateActivityId('collection');
      addActivityToPlayer(
        player,
        collectionId,
        `${collectionType.charAt(0).toUpperCase() + collectionType.slice(1)} Collection`,
        'hobby',
        `Casually collecting ${collectionType}`,
        `${collectionType}_casual`,
        5 // Smaller collection
      );

      return {
        type: 'messageEvent',
        message: `You casually pick up ${collectionType} when you find interesting ones. It's a fun, low-pressure hobby.`,
      };
    } else if (selectedOption === 2) {
      if ((c.money ?? 0) < 75) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money to join the collector community.",
        };
      }
      c.money = (c.money ?? 0) - 75;
      c.happiness = Math.min(100, (c.happiness ?? 50) + 25);
      c.social = Math.min(100, (c.social ?? 50) + 15);
      c.prestige = (c.prestige ?? 0) + 10;

      // Create community-based collection activity
      const collectionId = generateActivityId('collection');
      addActivityToPlayer(
        player,
        collectionId,
        `${collectionType.charAt(0).toUpperCase() + collectionType.slice(1)} Collector Community`,
        'hobby',
        `Collecting ${collectionType} with a community`,
        `${collectionType}_community`,
        15 // Community helps expand collection
      );

      // Add follow-up event for collection showcase
      addOneTimeEventToPlayer(
        player,
        'Collection Showcase',
        `You showcased your ${collectionType} collection at a community event! Fellow collectors were impressed with your curated pieces. You've made valuable connections!`,
        120
      );

      return {
        type: 'messageEvent',
        message: `You've joined a collector community! You meet fellow enthusiasts and learn so much about ${collectionType}.`,
      };
    } else {
      c.happiness = Math.max(0, (c.happiness ?? 50) - 5);
      return {
        type: 'messageEvent',
        message: "You decide collecting isn't for you right now.",
      };
    }
  }
}

/**
 * Reading Challenge Event - Enhanced with habit and progress tracking
 */
export class ReadingChallengeEvent extends BaseEvent {
  readonly id = 'readingChallenge';

  getConfig(): EventConfig {
    return {
      minAge: 10,
      maxAge: 100,
      baseChance: 0.0002,
      triggerType: 'random',
    };
  }

  checkConditions(player: Player): boolean {
    const c = player.c;
    return (
      !player.askedQuestions.has(this.id) && c.ageYears >= 10 && c.ageYears <= 100
    );
  }

  getQuestion(): string {
    return 'Challenge yourself to read 50 books this year?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, 50 books!', data: 'fifty' },
      { option: 'More realistic: 20 books', data: 'twenty' },
      { option: 'Just read for fun', data: 'casual' },
      { option: 'Not a reader', data: 'no' },
    ];
  }

  processAnswer(player: Player, selectedOption: number): EventResult {
    const c = player.c;

    if (selectedOption === 0) {
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 30);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 20);
      c.stress = Math.max(0, (c.stress ?? 0) - 10);

      // Create reading challenge activity with ActivityRecord
      const readingId = generateActivityId('reading');
      addActivityToPlayer(
        player,
        readingId,
        'Reading Challenge: 50 Books',
        'hobby',
        'Reading 50 books this year',
        'ambitious_reader',
        0 // Books read so far
      );

      // Add reading habit
      addHabitToPlayer(player, 'daily_reading');

      // Create daily reading schedule
      addScheduleToPlayer(player, 'Reading time', ['daily'], 45);

      // Create multiple check-in events throughout the year
      addOneTimeEventToPlayer(
        player,
        'Reading Challenge: Quarter Mark',
        "You've been reading consistently for 3 months! You're making great progress toward your 50-book goal. Keep it up!",
        90
      );

      addOneTimeEventToPlayer(
        player,
        'Reading Challenge: Halfway There',
        "You're halfway through the year! Your reading habit has become a cherished part of your daily routine. The books you've read have opened your mind to new ideas and perspectives.",
        180
      );

      return {
        type: 'messageEvent',
        message:
          "You've committed to reading 50 books this year! An ambitious goal that will expand your mind.",
      };
    } else if (selectedOption === 1) {
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 20);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 15);
      c.stress = Math.max(0, (c.stress ?? 0) - 5);

      // Create moderate reading challenge activity
      const readingId = generateActivityId('reading');
      addActivityToPlayer(
        player,
        readingId,
        'Reading Challenge: 20 Books',
        'hobby',
        'Reading 20 books this year',
        'moderate_reader',
        0 // Books read so far
      );

      // Add reading habit
      addHabitToPlayer(player, 'regular_reading');

      // Create reading schedule (few times per week)
      addScheduleToPlayer(player, 'Reading time', ['monday', 'wednesday', 'friday'], 30);

      // Add check-in event
      addOneTimeEventToPlayer(
        player,
        'Reading Challenge Check-in',
        "You've been keeping up with your reading goal! The books you've read have enriched your life in unexpected ways.",
        120
      );

      return {
        type: 'messageEvent',
        message:
          "You've set a goal to read 20 books this year. A realistic target that will still enrich your life.",
      };
    } else if (selectedOption === 2) {
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 10);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 10);

      // Create casual reading activity (no strict tracking)
      const readingId = generateActivityId('reading');
      addActivityToPlayer(
        player,
        readingId,
        'Casual Reading',
        'hobby',
        'Reading for pleasure without pressure',
        'casual_reader',
        0
      );

      return {
        type: 'messageEvent',
        message:
          'You decide to read casually without pressure. Sometimes the best reading happens when it\'s purely for enjoyment.',
      };
    } else {
      c.happiness = Math.max(0, (c.happiness ?? 50) - 5);
      return {
        type: 'messageEvent',
        message: "Reading just isn't your thing.",
      };
    }
  }
}

/**
 * Fishing Hobby Event
 */
export class FishingHobbyEvent extends BaseEvent {
  readonly id = 'fishingHobby';

  getConfig(): EventConfig {
    return {
      minAge: 10,
      maxAge: 100,
      baseChance: 0.0002,
      triggerType: 'random',
    };
  }

  checkConditions(player: Player): boolean {
    const c = player.c;
    return (
      !player.askedQuestions.has(this.id) && c.ageYears >= 10 && c.ageYears <= 100
    );
  }

  getQuestion(): string {
    return 'You\'re interested in taking up fishing. Start?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Buy fishing gear!', data: 'gear', moneyCost: 150 },
      { option: 'Borrow equipment first', data: 'borrow' },
      { option: 'Join a fishing club', data: 'club', moneyCost: 75 },
      { option: 'Not interested', data: 'no' },
    ];
  }

  processAnswer(player: Player, selectedOption: number): EventResult {
    const c = player.c;

    if (selectedOption === 0) {
      if ((c.money ?? 0) < 150) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for fishing gear right now.",
        };
      }
      c.money = (c.money ?? 0) - 150;
      c.happiness = Math.min(100, (c.happiness ?? 50) + 25);
      c.stress = Math.max(0, (c.stress ?? 0) - 20);

      // Create fishing activity
      const fishingId = generateActivityId('fishing');
      addActivityToPlayer(
        player,
        fishingId,
        'Fishing',
        'hobby',
        'Recreational fishing with quality gear',
        'serious_angler',
        50
      );

      // Add fishing schedule
      addScheduleToPlayer(player, 'Fishing', ['saturday'], 120, `lake${c.id}`);

      return {
        type: 'messageEvent',
        message:
          'You invested in quality fishing gear! There\'s something deeply peaceful about waiting by the water. Fishing becomes your way to unwind.',
      };
    } else if (selectedOption === 1) {
      c.happiness = Math.min(100, (c.happiness ?? 50) + 15);
      c.stress = Math.max(0, (c.stress ?? 0) - 10);

      return {
        type: 'messageEvent',
        message:
          'You borrowed some fishing equipment to try it out. It\'s relaxing being out on the water!',
      };
    } else if (selectedOption === 2) {
      if ((c.money ?? 0) < 75) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for the fishing club membership.",
        };
      }
      c.money = (c.money ?? 0) - 75;
      c.happiness = Math.min(100, (c.happiness ?? 50) + 20);
      c.social = Math.min(100, (c.social ?? 50) + 15);
      c.stress = Math.max(0, (c.stress ?? 0) - 15);

      // Create fishing club activity
      const fishingId = generateActivityId('fishing');
      addActivityToPlayer(
        player,
        fishingId,
        'Fishing Club',
        'hobby',
        'Fishing with a local group',
        'social_angler',
        50
      );

      // Add fishing club schedule
      addScheduleToPlayer(player, 'Fishing Club', ['saturday'], 180, `lake${c.id}`);

      return {
        type: 'messageEvent',
        message:
          'You joined a local fishing club! You learn techniques from experienced anglers and make new friends who share your love of the outdoors.',
      };
    } else {
      c.happiness = Math.max(0, (c.happiness ?? 50) - 5);
      return {
        type: 'messageEvent',
        message: 'Fishing doesn\'t appeal to you right now.',
      };
    }
  }
}

/**
 * Puzzle & Board Games Event
 */
export class PuzzleHobbyEvent extends BaseEvent {
  readonly id = 'puzzleHobby';

  private getPuzzleType(): string {
    const types = [
      'jigsaw puzzles',
      'crossword puzzles',
      'Sudoku',
      'chess',
      'strategy board games',
      'escape room puzzles',
    ];
    return types[Math.floor(Math.random() * types.length)];
  }

  getConfig(): EventConfig {
    return {
      minAge: 8,
      maxAge: 100,
      baseChance: 0.0002,
      triggerType: 'random',
    };
  }

  checkConditions(player: Player): boolean {
    const c = player.c;
    return (
      !player.askedQuestions.has(this.id) && c.ageYears >= 8 && c.ageYears <= 100
    );
  }

  getQuestion(): string {
    const puzzleType = this.getPuzzleType();
    return `You want to get into ${puzzleType}. Start?`;
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, challenge my brain!', data: 'serious', moneyCost: 30 },
      { option: 'Try it casually', data: 'casual' },
      { option: 'Join a puzzle group', data: 'group', moneyCost: 25 },
      { option: 'Not interested', data: 'no' },
    ];
  }

  processAnswer(player: Player, selectedOption: number): EventResult {
    const c = player.c;
    const puzzleType = this.getPuzzleType();

    if (selectedOption === 0) {
      if ((c.money ?? 0) < 30) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for puzzle supplies right now.",
        };
      }
      c.money = (c.money ?? 0) - 30;
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 15);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 15);
      c.stress = Math.max(0, (c.stress ?? 0) - 10);

      return {
        type: 'messageEvent',
        message: `You started doing ${puzzleType} regularly! The mental challenge is satisfying, and you can feel your problem-solving skills sharpening.`,
      };
    } else if (selectedOption === 1) {
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 8);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 10);

      return {
        type: 'messageEvent',
        message: `You try ${puzzleType} when you have time. It's a nice way to keep your mind active.`,
      };
    } else if (selectedOption === 2) {
      if ((c.money ?? 0) < 25) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for the puzzle group membership.",
        };
      }
      c.money = (c.money ?? 0) - 25;
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 12);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 15);
      c.social = Math.min(100, (c.social ?? 50) + 15);

      return {
        type: 'messageEvent',
        message: `You joined a puzzle group! You meet regularly to tackle ${puzzleType} together. It's great mental stimulation and good company.`,
      };
    } else {
      c.happiness = Math.max(0, (c.happiness ?? 50) - 5);
      return {
        type: 'messageEvent',
        message: 'Puzzles aren\'t really your thing.',
      };
    }
  }
}

/**
 * Astronomy/Stargazing Event
 */
export class AstronomyHobbyEvent extends BaseEvent {
  readonly id = 'astronomyHobby';

  getConfig(): EventConfig {
    return {
      minAge: 10,
      maxAge: 100,
      baseChance: 0.0002,
      triggerType: 'random',
    };
  }

  checkConditions(player: Player): boolean {
    const c = player.c;
    return (
      !player.askedQuestions.has(this.id) && c.ageYears >= 10 && c.ageYears <= 100
    );
  }

  getQuestion(): string {
    return 'You\'re fascinated by the night sky. Take up stargazing?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Buy a telescope!', data: 'telescope', moneyCost: 200 },
      { option: 'Use a stargazing app', data: 'app' },
      { option: 'Join an astronomy club', data: 'club', moneyCost: 50 },
      { option: 'Not interested', data: 'no' },
    ];
  }

  processAnswer(player: Player, selectedOption: number): EventResult {
    const c = player.c;

    if (selectedOption === 0) {
      if ((c.money ?? 0) < 200) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for a telescope right now.",
        };
      }
      c.money = (c.money ?? 0) - 200;
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 20);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 20);
      c.stress = Math.max(0, (c.stress ?? 0) - 15);

      // Create astronomy activity
      const astronomyId = generateActivityId('astronomy');
      addActivityToPlayer(
        player,
        astronomyId,
        'Stargazing',
        'hobby',
        'Observing celestial objects with telescope',
        'serious_astronomer',
        50
      );

      return {
        type: 'messageEvent',
        message:
          'You invested in a telescope! Gazing at the moon, planets, and distant galaxies fills you with wonder. The universe is breathtaking!',
      };
    } else if (selectedOption === 1) {
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 10);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 10);
      c.stress = Math.max(0, (c.stress ?? 0) - 5);

      return {
        type: 'messageEvent',
        message:
          'You downloaded a stargazing app and started learning constellations. Even without a telescope, the night sky is amazing!',
      };
    } else if (selectedOption === 2) {
      if ((c.money ?? 0) < 50) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for the astronomy club membership.",
        };
      }
      c.money = (c.money ?? 0) - 50;
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 15);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 15);
      c.social = Math.min(100, (c.social ?? 50) + 15);

      // Create astronomy club activity
      const astronomyId = generateActivityId('astronomy');
      addActivityToPlayer(
        player,
        astronomyId,
        'Astronomy Club',
        'hobby',
        'Stargazing with a local group',
        'social_astronomer',
        50
      );

      return {
        type: 'messageEvent',
        message:
          'You joined an astronomy club! You attend star parties, use shared telescopes, and learn from experienced astronomers. The cosmos has never seemed more accessible!',
      };
    } else {
      c.happiness = Math.max(0, (c.happiness ?? 50) - 5);
      return {
        type: 'messageEvent',
        message: 'Stargazing doesn\'t capture your interest right now.',
      };
    }
  }
}

/**
 * Podcasting/Content Creation Event
 */
export class ContentCreationEvent extends BaseEvent {
  readonly id = 'contentCreation';

  private getContentType(): string {
    const types = ['podcast', 'YouTube channel', 'blog', 'TikTok', 'newsletter'];
    return types[Math.floor(Math.random() * types.length)];
  }

  getConfig(): EventConfig {
    return {
      minAge: 14,
      maxAge: 65,
      baseChance: 0.0002,
      triggerType: 'random',
    };
  }

  checkConditions(player: Player): boolean {
    const c = player.c;
    return (
      !player.askedQuestions.has(this.id) && c.ageYears >= 14 && c.ageYears <= 65
    );
  }

  getQuestion(): string {
    const contentType = this.getContentType();
    return `You want to start a ${contentType}. Begin creating content?`;
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, invest in equipment!', data: 'professional', moneyCost: 200, energyCost: 15 },
      { option: 'Start with what I have', data: 'basic' },
      { option: 'Not interested', data: 'no' },
    ];
  }

  processAnswer(player: Player, selectedOption: number): EventResult {
    const c = player.c;
    const contentType = this.getContentType();

    if (selectedOption === 0) {
      if ((c.money ?? 0) < 200) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for content creation equipment right now.",
        };
      }
      c.money = (c.money ?? 0) - 200;
      c.energy = Math.max(0, c.energy - 15);
      c.creativity = Math.min(100, (c.creativity ?? 50) + 20);
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 10);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 20);
      c.prestige = (c.prestige ?? 0) + 5;

      // Create content creation activity
      const contentId = generateActivityId('content');
      addActivityToPlayer(
        player,
        contentId,
        `${contentType.charAt(0).toUpperCase() + contentType.slice(1)} Creator`,
        'hobby',
        `Creating content for ${contentType}`,
        'professional_creator',
        50
      );

      // Chance to earn some money from content
      if (Math.random() < 0.2) {
        const earnings = Math.floor(Math.random() * 50) + 20;
        c.money = (c.money ?? 0) + earnings;
        return {
          type: 'messageEvent',
          message: `You launched your ${contentType} with quality equipment! Your content is getting attention, and you even earned $${earnings} from early sponsorships!`,
        };
      }

      return {
        type: 'messageEvent',
        message: `You launched your ${contentType} with professional equipment! Creating content is challenging but rewarding. You're building an audience!`,
      };
    } else if (selectedOption === 1) {
      c.creativity = Math.min(100, (c.creativity ?? 50) + 12);
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 5);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 15);

      return {
        type: 'messageEvent',
        message: `You started your ${contentType} with basic equipment. Content quality grows as you learn, and you're having fun expressing yourself!`,
      };
    } else {
      c.happiness = Math.max(0, (c.happiness ?? 50) - 5);
      return {
        type: 'messageEvent',
        message: 'Creating content isn\'t for you right now.',
      };
    }
  }
}

/**
 * Cooking/Baking as Hobby Event
 */
export class CookingHobbyEvent extends BaseEvent {
  readonly id = 'cookingHobby';

  private getCuisineType(): string {
    const cuisines = [
      'Italian',
      'Japanese',
      'Mexican',
      'Thai',
      'French pastry',
      'BBQ and grilling',
      'vegan cooking',
      'bread baking',
    ];
    return cuisines[Math.floor(Math.random() * cuisines.length)];
  }

  getConfig(): EventConfig {
    return {
      minAge: 14,
      maxAge: 100,
      baseChance: 0.0002,
      triggerType: 'random',
    };
  }

  checkConditions(player: Player): boolean {
    const c = player.c;
    return (
      !player.askedQuestions.has(this.id) && c.ageYears >= 14 && c.ageYears <= 100
    );
  }

  getQuestion(): string {
    const cuisine = this.getCuisineType();
    return `You want to learn ${cuisine} cooking. Start?`;
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Buy cookbooks and ingredients!', data: 'books', moneyCost: 80 },
      { option: 'Watch cooking videos', data: 'videos' },
      { option: 'Take a cooking class', data: 'class', moneyCost: 120 },
      { option: 'Not interested', data: 'no' },
    ];
  }

  processAnswer(player: Player, selectedOption: number): EventResult {
    const c = player.c;
    const cuisine = this.getCuisineType();

    if (selectedOption === 0) {
      if ((c.money ?? 0) < 80) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for cookbooks and ingredients right now.",
        };
      }
      c.money = (c.money ?? 0) - 80;
      c.creativity = Math.min(100, (c.creativity ?? 50) + 15);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 20);
      c.health = Math.min(100, c.health + 5); // Healthier homemade food

      // Create cooking activity
      const cookingId = generateActivityId('cooking');
      addActivityToPlayer(
        player,
        cookingId,
        `${cuisine} Cooking`,
        'hobby',
        `Learning ${cuisine} cuisine`,
        'home_chef',
        50
      );

      return {
        type: 'messageEvent',
        message: `You bought cookbooks and quality ingredients for ${cuisine}! Your kitchen experiments are becoming delicious successes.`,
      };
    } else if (selectedOption === 1) {
      c.creativity = Math.min(100, (c.creativity ?? 50) + 8);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 10);

      return {
        type: 'messageEvent',
        message: `You started learning ${cuisine} from online videos. It's fun trying new recipes and techniques!`,
      };
    } else if (selectedOption === 2) {
      if ((c.money ?? 0) < 120) {
        return {
          type: 'messageEvent',
          message: "You don't have enough money for a cooking class right now.",
        };
      }
      c.money = (c.money ?? 0) - 120;
      c.creativity = Math.min(100, (c.creativity ?? 50) + 20);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 20);
      c.social = Math.min(100, (c.social ?? 50) + 10);

      // Create cooking class activity
      const cookingId = generateActivityId('cooking');
      addActivityToPlayer(
        player,
        cookingId,
        `${cuisine} Cooking Class`,
        'hobby',
        `Learning ${cuisine} from a professional chef`,
        'culinary_student',
        60
      );

      // Add cooking class schedule
      addScheduleToPlayer(player, `${cuisine} Cooking Class`, ['wednesday'], 120);

      return {
        type: 'messageEvent',
        message: `You enrolled in a ${cuisine} cooking class! Learning from a chef and cooking with others is an amazing experience.`,
      };
    } else {
      c.happiness = Math.max(0, (c.happiness ?? 50) - 5);
      return {
        type: 'messageEvent',
        message: 'Cooking as a hobby doesn\'t interest you right now.',
      };
    }
  }
}

// Export all hobby events
export const hobbyEvents = [
  new GardeningEvent(),
  new MeditationEvent(),
  new BirdWatchingEvent(),
  new CollectionHobbyEvent(),
  new ReadingChallengeEvent(),
  new FishingHobbyEvent(),
  new PuzzleHobbyEvent(),
  new AstronomyHobbyEvent(),
  new ContentCreationEvent(),
  new CookingHobbyEvent(),
];
