/**
 * Physical & Sports Activity Events
 * Exercise, sports, and fitness activities
 */

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

/**
 * Join Soccer Team Event
 */
export class JoinSoccerTeamEvent extends BaseEvent {
  readonly id = 'joinSoccerTeam';

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

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

  getQuestion(): string {
    return 'A local soccer team is looking for new members. Join?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, I love soccer!', energyCost: 20 },
      { option: 'Try a practice first', energyCost: 10 },
      { option: 'Not my thing' },
    ];
  }

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

    if (selectedOption === 0) {
      c.energy = Math.max(0, c.energy - 20);
      c.health = Math.min(100, c.health + 10);
      c.social = Math.min(100, (c.social ?? 50) + 15);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 15);

      return {
        type: 'messageEvent',
        message: 'You join the soccer team! It\'s great exercise and you make new friends.',
      };
    } else if (selectedOption === 1) {
      c.energy = Math.max(0, c.energy - 10);
      c.health = Math.min(100, c.health + 5);
      c.social = Math.min(100, (c.social ?? 50) + 5);

      return {
        type: 'messageEvent',
        message: 'You try a practice session. It was fun, you might come back!',
      };
    } else {
      return {
        type: 'messageEvent',
        message: 'You decide soccer isn\'t for you right now.',
      };
    }
  }
}

/**
 * Learn Martial Arts Event
 */
export class LearnMartialArtsEvent extends BaseEvent {
  readonly id = 'learnMartialArts';

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

  checkConditions(player: Player): boolean {
    const c = player.c;
    return (
      !player.askedQuestions.has(this.id) &&
      c.ageYears >= 6 &&
      c.ageYears <= 60 &&
      c.energy >= 15 &&
      c.health >= 40
    );
  }

  getQuestion(): string {
    return 'A martial arts dojo is offering beginner classes. Sign up?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, learn to defend myself!', moneyCost: 100, energyCost: 20 },
      { option: 'Try a free class first', energyCost: 15 },
      { option: 'Not interested' },
    ];
  }

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

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

      return {
        type: 'messageEvent',
        message: 'You start martial arts training. You feel disciplined and confident!',
      };
    } else if (selectedOption === 1) {
      c.energy = Math.max(0, c.energy - 15);
      c.health = Math.min(100, c.health + 5);

      return {
        type: 'messageEvent',
        message: 'You try a free class. It was challenging but interesting!',
      };
    } else {
      return {
        type: 'messageEvent',
        message: 'You pass on the martial arts opportunity.',
      };
    }
  }
}

/**
 * Running Habit Event
 */
export class RunningHabitEvent extends BaseEvent {
  readonly id = 'runningHabit';

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

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

  getQuestion(): string {
    return 'You\'re thinking about starting a running habit. Start?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, let\'s get fit!', energyCost: 20 },
      { option: 'Start with short jogs', energyCost: 10 },
      { option: 'Maybe later' },
    ];
  }

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

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

      return {
        type: 'messageEvent',
        message: 'You start running regularly. Your fitness improves significantly!',
      };
    } else if (selectedOption === 1) {
      c.energy = Math.max(0, c.energy - 10);
      c.health = Math.min(100, c.health + 8);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 5);

      return {
        type: 'messageEvent',
        message: 'You start with short jogs. It\'s a good beginning!',
      };
    } else {
      return {
        type: 'messageEvent',
        message: 'You decide to postpone starting a running routine.',
      };
    }
  }
}

/**
 * Join Gym Event
 */
export class JoinGymEvent extends BaseEvent {
  readonly id = 'joinGym';

  getConfig(): EventConfig {
    return {
      minAge: 15,
      maxAge: 70,
      baseChance: 0.0003,
      triggerType: 'random',
    };
  }

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

  getQuestion(): string {
    return 'A gym nearby is offering a membership deal. Join?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, get in shape!', moneyCost: 50 },
      { option: 'Try a day pass first', moneyCost: 10 },
      { option: 'Not interested' },
    ];
  }

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

    if (selectedOption === 0) {
      c.money -= 50;
      c.health = Math.min(100, c.health + 15);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 10);
      c.stress = Math.max(0, (c.stress ?? 0) - 15);

      return {
        type: 'messageEvent',
        message: 'You join the gym and start working out regularly!',
      };
    } else if (selectedOption === 1) {
      c.money -= 10;
      c.health = Math.min(100, c.health + 5);

      return {
        type: 'messageEvent',
        message: 'You try the gym for a day. It was a good workout!',
      };
    } else {
      return {
        type: 'messageEvent',
        message: 'You decide the gym isn\'t for you right now.',
      };
    }
  }
}

/**
 * Yoga Class Event
 */
export class YogaClassEvent extends BaseEvent {
  readonly id = 'yogaClass';

  getConfig(): EventConfig {
    return {
      minAge: 12,
      maxAge: 80,
      baseChance: 0.0003,
      triggerType: 'random',
    };
  }

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

  getQuestion(): string {
    return 'A yoga studio is offering classes. Would you like to try?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, find my zen!', moneyCost: 30, energyCost: 15 },
      { option: 'Try one class', energyCost: 10 },
      { option: 'Not my style' },
    ];
  }

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

    if (selectedOption === 0) {
      c.money -= 30;
      c.energy = Math.max(0, c.energy - 15);
      c.health = Math.min(100, c.health + 10);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 15);
      c.stress = Math.max(0, (c.stress ?? 0) - 25);

      return {
        type: 'messageEvent',
        message: 'You start practicing yoga regularly. You feel more balanced and peaceful!',
      };
    } else if (selectedOption === 1) {
      c.energy = Math.max(0, c.energy - 10);
      c.health = Math.min(100, c.health + 5);
      c.stress = Math.max(0, (c.stress ?? 0) - 10);

      return {
        type: 'messageEvent',
        message: 'You try a yoga class. It was relaxing!',
      };
    } else {
      return {
        type: 'messageEvent',
        message: 'You decide yoga isn\'t for you.',
      };
    }
  }
}

/**
 * Swimming Lessons Event
 */
export class SwimmingLessonsEvent extends BaseEvent {
  readonly id = 'swimmingLessons';

  getConfig(): EventConfig {
    return {
      minAge: 5,
      maxAge: 70,
      baseChance: 0.0003,
      triggerType: 'random',
    };
  }

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

  getQuestion(): string {
    return 'A local pool is offering swimming lessons. Take them?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, learn to swim!', moneyCost: 75, energyCost: 15 },
      { option: 'Try a trial class', energyCost: 10 },
      { option: 'Not interested' },
    ];
  }

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

    if (selectedOption === 0) {
      c.money -= 75;
      c.energy = Math.max(0, c.energy - 15);
      c.health = Math.min(100, c.health + 15);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 15);
      c.stress = Math.max(0, (c.stress ?? 0) - 10);

      return {
        type: 'messageEvent',
        message: 'You signed up for swimming lessons! Swimming is great for your health and a valuable life skill.',
      };
    } else if (selectedOption === 1) {
      c.energy = Math.max(0, c.energy - 10);
      c.health = Math.min(100, c.health + 5);

      return {
        type: 'messageEvent',
        message: 'You tried a swimming lesson. It was fun getting in the water!',
      };
    } else {
      return {
        type: 'messageEvent',
        message: 'You decided swimming lessons aren\'t for you right now.',
      };
    }
  }
}

/**
 * Dance Class Event
 */
export class DanceClassEvent extends BaseEvent {
  readonly id = 'danceClass';

  private getDanceStyle(age: number): string {
    if (age < 12) {
      const styles = ['ballet', 'hip-hop', 'jazz'];
      return styles[Math.floor(Math.random() * styles.length)];
    } else if (age < 25) {
      const styles = ['hip-hop', 'contemporary', 'Latin', 'breakdancing', 'K-pop'];
      return styles[Math.floor(Math.random() * styles.length)];
    } else {
      const styles = ['salsa', 'ballroom', 'tango', 'swing', 'contemporary'];
      return styles[Math.floor(Math.random() * styles.length)];
    }
  }

  getConfig(): EventConfig {
    return {
      minAge: 6,
      maxAge: 70,
      baseChance: 0.0003,
      triggerType: 'random',
    };
  }

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

  getQuestion(player?: Player): string {
    const style = player ? this.getDanceStyle(player.c.ageYears) : 'dance';
    return `There's a ${style} dance class starting soon. Join?`;
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, let\'s dance!', moneyCost: 60, energyCost: 20 },
      { option: 'Try a beginner session', energyCost: 15 },
      { option: 'Not my thing' },
    ];
  }

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

    if (selectedOption === 0) {
      c.money -= 60;
      c.energy = Math.max(0, c.energy - 20);
      c.health = Math.min(100, c.health + 12);
      c.social = Math.min(100, (c.social ?? 50) + 15);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 20);
      c.stress = Math.max(0, (c.stress ?? 0) - 15);

      return {
        type: 'messageEvent',
        message: `You signed up for ${style} dance classes! Dancing is great exercise and a wonderful way to express yourself.`,
      };
    } else if (selectedOption === 1) {
      c.energy = Math.max(0, c.energy - 15);
      c.health = Math.min(100, c.health + 5);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 10);

      return {
        type: 'messageEvent',
        message: `You tried a beginner ${style} class. It was fun to move to the music!`,
      };
    } else {
      return {
        type: 'messageEvent',
        message: 'Dancing isn\'t really your thing. You pass on the class.',
      };
    }
  }
}

/**
 * Cycling/Biking Event
 */
export class CyclingEvent extends BaseEvent {
  readonly id = 'cyclingHobby';

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

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

  getQuestion(): string {
    return 'You\'re thinking about taking up cycling. Get a bike?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Buy a quality bike!', moneyCost: 300 },
      { option: 'Get a used bike', moneyCost: 80 },
      { option: 'Rent bikes occasionally', moneyCost: 20 },
      { option: 'Not interested' },
    ];
  }

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

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

      return {
        type: 'messageEvent',
        message: 'You invested in a quality bike! Cycling becomes a regular part of your routine. You explore new trails and enjoy the fresh air.',
      };
    } else if (selectedOption === 1) {
      c.money -= 80;
      c.health = Math.min(100, c.health + 12);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 10);
      c.stress = Math.max(0, (c.stress ?? 0) - 10);

      return {
        type: 'messageEvent',
        message: 'You found a decent used bike! It\'s not fancy, but it gets you riding and enjoying the outdoors.',
      };
    } else if (selectedOption === 2) {
      c.money -= 20;
      c.health = Math.min(100, c.health + 5);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 5);

      return {
        type: 'messageEvent',
        message: 'You rent bikes now and then for fun rides. It\'s a nice occasional activity without the commitment.',
      };
    } else {
      return {
        type: 'messageEvent',
        message: 'Cycling doesn\'t appeal to you right now. Maybe another time.',
      };
    }
  }
}

/**
 * Rock Climbing Event
 */
export class RockClimbingEvent extends BaseEvent {
  readonly id = 'rockClimbing';

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

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

  getQuestion(): string {
    return 'A climbing gym just opened nearby. Try rock climbing?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, get a membership!', moneyCost: 80, energyCost: 25 },
      { option: 'Try a day pass', moneyCost: 20, energyCost: 20 },
      { option: 'Not my thing' },
    ];
  }

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

    if (selectedOption === 0) {
      c.money -= 80;
      c.energy = Math.max(0, c.energy - 25);
      c.health = Math.min(100, c.health + 18);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 20);
      c.stress = Math.max(0, (c.stress ?? 0) - 10);
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 5); // Problem-solving

      return {
        type: 'messageEvent',
        message: 'You joined the climbing gym! Rock climbing is an amazing full-body workout that challenges both your body and mind.',
      };
    } else if (selectedOption === 1) {
      c.money -= 20;
      c.energy = Math.max(0, c.energy - 20);
      c.health = Math.min(100, c.health + 8);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 10);

      return {
        type: 'messageEvent',
        message: 'You tried rock climbing with a day pass. It was challenging but exhilarating!',
      };
    } else {
      return {
        type: 'messageEvent',
        message: 'Rock climbing seems too intense. You pass on the opportunity.',
      };
    }
  }
}

// Export all physical events
export const physicalEvents = [
  new JoinSoccerTeamEvent(),
  new LearnMartialArtsEvent(),
  new RunningHabitEvent(),
  new JoinGymEvent(),
  new YogaClassEvent(),
  new SwimmingLessonsEvent(),
  new DanceClassEvent(),
  new CyclingEvent(),
  new RockClimbingEvent(),
];
