/**
 * Career & Work Activity Events
 * Professional development and work-related activities (ages 18-65)
 */

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

/**
 * Professional Conference Event
 */
export class ProfessionalConferenceEvent extends BaseEvent {
  readonly id = 'professionalConference';

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

  checkConditions(player: Player): boolean {
    const c = player.c;
    return (
      !player.askedQuestions.has(this.id) &&
      c.ageYears >= 22 &&
      c.ageYears <= 65 &&
      c.occupation === 'work' &&
      !!c.job &&
      c.money >= 100
    );
  }

  getQuestion(): string {
    return 'A professional conference in your field is happening next month. Attend?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, great networking!', moneyCost: 500 },
      { option: 'Virtual attendance only', moneyCost: 100 },
      { option: 'Skip it' },
    ];
  }

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

    // Find job ActivityRecord
    const jobRecord = c.activityRecords?.find(
      (r: { type: string; id: string }) => r.type === 'job' && r.id === c.job?.id
    );

    if (selectedOption === 0) {
      // In-person attendance
      c.money -= 500;
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 20);
      c.social = Math.min(100, (c.social ?? 50) + 25);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 15);

      if (jobRecord) {
        jobRecord.performance = Math.min(100, (jobRecord.performance ?? 50) + 15);
        const achievement = `Attended ${c.job?.title ?? 'Professional'} Conference`;
        if (!jobRecord.achievements?.includes(achievement)) {
          jobRecord.achievements = jobRecord.achievements ?? [];
          jobRecord.achievements.push(achievement);
        }
      }

      return {
        type: 'messageEvent',
        message: 'You attend the conference in person. The networking opportunities are amazing!',
      };
    } else if (selectedOption === 1) {
      // Virtual attendance
      c.money -= 100;
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 15);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 10);

      if (jobRecord) {
        jobRecord.performance = Math.min(100, (jobRecord.performance ?? 50) + 8);
      }

      return {
        type: 'messageEvent',
        message: 'You attend virtually. You still gain valuable knowledge.',
      };
    } else {
      // Skip
      c.happiness = Math.max(0, (c.happiness ?? 50) - 5);

      if (jobRecord) {
        jobRecord.performance = Math.max(0, (jobRecord.performance ?? 50) - 3);
      }

      return {
        type: 'messageEvent',
        message: 'You skip the conference. You might have missed some opportunities.',
      };
    }
  }
}

/**
 * Mentor Junior Event
 */
export class MentorJuniorEvent extends BaseEvent {
  readonly id = 'mentorJunior';

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

  checkConditions(player: Player): boolean {
    const c = player.c;
    const jobRecord = c.activityRecords?.find(
      (r: { type: string; id: string }) => r.type === 'job' && r.id === c.job?.id
    );

    const performanceCheck =
      !jobRecord ||
      ((jobRecord.performance ?? 50) >= 40 && jobRecord.focus !== 'Slack Off');

    return (
      !player.askedQuestions.has(this.id) &&
      c.ageYears >= 30 &&
      c.ageYears <= 65 &&
      c.occupation === 'work' &&
      !!c.job &&
      performanceCheck &&
      c.energy >= 10
    );
  }

  getQuestion(): string {
    return 'A junior colleague asks if you\'d be willing to mentor them. Accept?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, happy to help!', energyCost: 10 },
      { option: 'Maybe occasionally', energyCost: 5 },
      { option: 'Too busy right now' },
    ];
  }

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

    const jobRecord = c.activityRecords?.find(
      (r: { type: string; id: string }) => r.type === 'job' && r.id === c.job?.id
    );

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

      if (jobRecord) {
        jobRecord.performance = Math.min(100, (jobRecord.performance ?? 50) + 10);
        const achievement = 'Mentored a Junior Colleague';
        if (!jobRecord.achievements?.includes(achievement)) {
          jobRecord.achievements = jobRecord.achievements ?? [];
          jobRecord.achievements.push(achievement);
        }
      }

      return {
        type: 'messageEvent',
        message: 'You become a mentor. Helping others grow feels rewarding!',
      };
    } else if (selectedOption === 1) {
      c.energy = Math.max(0, c.energy - 5);
      c.social = Math.min(100, (c.social ?? 50) + 10);
      c.happiness = Math.min(100, (c.happiness ?? 50) + 5);

      if (jobRecord) {
        jobRecord.performance = Math.min(100, (jobRecord.performance ?? 50) + 5);
      }

      return {
        type: 'messageEvent',
        message: 'You offer occasional guidance. The colleague appreciates your help.',
      };
    } else {
      c.happiness = Math.max(0, (c.happiness ?? 50) - 5);

      return {
        type: 'messageEvent',
        message: 'You decline. The colleague is disappointed but understands.',
      };
    }
  }
}

/**
 * Side Hustle Event
 */
export class SideHustleEvent extends BaseEvent {
  readonly id = 'sideHustle';

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

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

  getQuestion(): string {
    return 'You have an idea for a side business. Start it?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Go all in!', moneyCost: 500, energyCost: 30 },
      { option: 'Start small', moneyCost: 100, energyCost: 15 },
      { option: 'Not the right time' },
    ];
  }

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

    if (selectedOption === 0) {
      c.money -= 500;
      c.energy = Math.max(0, c.energy - 30);

      // Random success (higher investment = better odds)
      const success = Math.random() > 0.4;
      if (success) {
        c.money += 1500;
        c.intelligence = Math.min(100, (c.intelligence ?? 50) + 15);
        c.happiness = Math.min(100, (c.happiness ?? 50) + 25);

        return {
          type: 'messageEvent',
          message: 'Your side hustle takes off! You make a great profit.',
        };
      } else {
        c.stress = Math.min(100, (c.stress ?? 0) + 20);

        return {
          type: 'messageEvent',
          message: 'Your side hustle struggles. It\'s a learning experience.',
        };
      }
    } else if (selectedOption === 1) {
      c.money -= 100;
      c.energy = Math.max(0, c.energy - 15);

      const success = Math.random() > 0.5;
      if (success) {
        c.money += 300;
        c.intelligence = Math.min(100, (c.intelligence ?? 50) + 10);
        c.happiness = Math.min(100, (c.happiness ?? 50) + 15);

        return {
          type: 'messageEvent',
          message: 'Your small side project earns some extra income!',
        };
      } else {
        c.stress = Math.min(100, (c.stress ?? 0) + 10);

        return {
          type: 'messageEvent',
          message: 'Your side project didn\'t work out, but you learned a lot.',
        };
      }
    } else {
      return {
        type: 'messageEvent',
        message: 'You decide to focus on your main job for now.',
      };
    }
  }
}

/**
 * Networking Event
 */
export class NetworkingEventEvent extends BaseEvent {
  readonly id = 'networkingEvent';

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

  checkConditions(player: Player): boolean {
    const c = player.c;
    return (
      !player.askedQuestions.has(this.id) &&
      c.ageYears >= 22 &&
      c.ageYears <= 65 &&
      c.occupation === 'work' &&
      c.energy >= 10
    );
  }

  getQuestion(): string {
    return 'There\'s a professional networking event tonight. Attend?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, great opportunity!', energyCost: 15 },
      { option: 'Stay for a bit', energyCost: 8 },
      { option: 'Skip it' },
    ];
  }

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

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

      // Chance to make a valuable connection
      if (Math.random() > 0.6) {
        c.prestige = (c.prestige ?? 0) + 5;
        return {
          type: 'messageEvent',
          message: 'You network extensively and make a valuable industry connection!',
        };
      }

      return {
        type: 'messageEvent',
        message: 'You meet interesting professionals and expand your network.',
      };
    } else if (selectedOption === 1) {
      c.energy = Math.max(0, c.energy - 8);
      c.social = Math.min(100, (c.social ?? 50) + 10);

      return {
        type: 'messageEvent',
        message: 'You make a brief appearance and chat with a few people.',
      };
    } else {
      return {
        type: 'messageEvent',
        message: 'You skip the event and relax at home instead.',
      };
    }
  }
}

/**
 * Professional Certification Event
 */
export class ProfessionalCertificationEvent extends BaseEvent {
  readonly id = 'professionalCertification';

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

  checkConditions(player: Player): boolean {
    const c = player.c;
    return (
      !player.askedQuestions.has(this.id) &&
      c.ageYears >= 22 &&
      c.ageYears <= 65 &&
      c.occupation === 'work' &&
      !!c.job &&
      c.money >= 200 &&
      c.energy >= 20
    );
  }

  getQuestion(): string {
    return 'You can get a professional certification in your field. Pursue it?';
  }

  getAnswerOptions(): AnswerOption[] {
    return [
      { option: 'Yes, invest in my career!', moneyCost: 800, energyCost: 30 },
      { option: 'Basic certification only', moneyCost: 300, energyCost: 15 },
      { option: 'Not worth it right now' },
    ];
  }

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

    const jobRecord = c.activityRecords?.find(
      (r: { type: string; id: string }) => r.type === 'job' && r.id === c.job?.id
    );

    if (selectedOption === 0) {
      c.money -= 800;
      c.energy = Math.max(0, c.energy - 30);
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 25);
      c.prestige = (c.prestige ?? 0) + 15;

      if (jobRecord) {
        jobRecord.performance = Math.min(100, (jobRecord.performance ?? 50) + 20);
        const achievement = `Advanced ${c.job?.title ?? 'Professional'} Certification`;
        if (!jobRecord.achievements?.includes(achievement)) {
          jobRecord.achievements = jobRecord.achievements ?? [];
          jobRecord.achievements.push(achievement);
        }
      }

      return {
        type: 'messageEvent',
        message: 'You earn an advanced professional certification! Your career prospects improve.',
      };
    } else if (selectedOption === 1) {
      c.money -= 300;
      c.energy = Math.max(0, c.energy - 15);
      c.intelligence = Math.min(100, (c.intelligence ?? 50) + 15);
      c.prestige = (c.prestige ?? 0) + 8;

      if (jobRecord) {
        jobRecord.performance = Math.min(100, (jobRecord.performance ?? 50) + 10);
      }

      return {
        type: 'messageEvent',
        message: 'You earn a basic certification. It\'s a good addition to your resume.',
      };
    } else {
      return {
        type: 'messageEvent',
        message: 'You decide to focus on other things for now.',
      };
    }
  }
}

// Export all career events
export const careerEvents = [
  new ProfessionalConferenceEvent(),
  new MentorJuniorEvent(),
  new SideHustleEvent(),
  new NetworkingEventEvent(),
  new ProfessionalCertificationEvent(),
];
