import { Person } from './person';
import { Mood, Interaction } from './types';
import { CrushError } from './errors';

export class Relationship {
    private person1: Person;
    private person2: Person;
    private interactionCount: number;
    private readonly MAX_INTERACTIONS = 5;
    private readonly interactions: Interaction[] = [
        'small talk',
        'awkward wave',
        'shared laugh',
        'accidental eye contact'
    ];

    constructor(person1: Person, person2: Person) {
        this.person1 = person1;
        this.person2 = person2;
        this.interactionCount = 0;
    }

    private getRandomInteraction(): Interaction {
        const index = Math.floor(Math.random() * this.interactions.length);
        return this.interactions[index];
    }

    private getRandomMood(): Mood {
        const moods: Mood[] = ['happy', 'nervous', 'flustered', 'overthinking'];
        const index = Math.floor(Math.random() * moods.length);
        return moods[index];
    }

    simulateInteraction(): string {
        this.interactionCount++;
        
        if (this.interactionCount > this.MAX_INTERACTIONS) {
            throw new CrushError("Too many interactions! Time to take a break and overthink everything.");
        }

        const interaction = this.getRandomInteraction();
        const mood = this.getRandomMood();
        this.person1.setMood(mood);

        const responses = [
            `${this.person1.getName()} ${interaction} with ${this.person2.getName()}. Current mood: ${mood}`,
            `During ${interaction}, ${this.person1.getName()} becomes ${mood}`,
            `Another awkward moment: ${this.person1.getName()} attempts ${interaction} and ends up ${mood}`,
            `${interaction} occurred! ${this.person1.getName()} is now feeling ${mood}`
        ];

        return `Interaction #${this.interactionCount}: ${responses[Math.floor(Math.random() * responses.length)]}`;
    }

    tryToDevelopFeelings(): never {
        const errorMessages = [
            "Error: Cannot process feelings.exe - Overthinking detected",
            "Critical failure: Butterflies in stomach causing system malfunction",
            "Fatal error: Brain.exe stopped working near crush",
            `Stack overflow: Too many thoughts about ${this.person2.getName()}`,
            "Exception: Heart.js failed to compile due to excess emotions",
            `Runtime Error: Failed to maintain composure in presence of ${this.person2.getName()}`,
            "System Failure: Nervous system overloaded with crush-induced anxiety"
        ];
        
        throw new CrushError(errorMessages[Math.floor(Math.random() * errorMessages.length)]);
    }

    getInteractionCount(): number {
        return this.interactionCount;
    }

    reset(): void {
        this.interactionCount = 0;
        this.person1.setMood('happy');
    }

    getCrushIntensity(): string {
        const intensity = Math.min((this.interactionCount / this.MAX_INTERACTIONS) * 100, 100);
        return `Crush intensity: ${intensity.toFixed(1)}%`;
    }
}