import { EconomicTracker } from './EconomicTracker';
import { AgentState } from './types';

export class Agent {
  economicTracker: EconomicTracker;
  skillLevel: number = 0.5;
  winRate: number = 0.5;
  unlockedFactors: string[] = [];
  learningStatus: { course: string; endDate: number; expectedImprovement: number } | null = null;

  constructor(
    public id: string,
    public role: string,
    public decisionCost: number,
    initialCapital: number
  ) {
    this.economicTracker = new EconomicTracker(initialCapital);
  }

  getWinRateModifier(): number {
    let modifier = 0;
    if (this.unlockedFactors.includes('moving_average_cross')) modifier += 0.02;
    if (this.unlockedFactors.includes('rsi_oversold')) modifier += 0.02;
    if (this.unlockedFactors.includes('bollinger_squeeze')) modifier += 0.03;
    if (this.unlockedFactors.includes('macd_divergence')) modifier += 0.04;
    if (this.unlockedFactors.includes('sentiment_momentum')) modifier += 0.05;
    if (this.unlockedFactors.includes('machine_learning_pred')) modifier += 0.08;
    return modifier;
  }

  getRiskModifier(): number {
    let modifier = 1.0;
    if (this.unlockedFactors.includes('atr_trailing_stop')) modifier *= 0.8; // Reduces loss by 20%
    if (this.unlockedFactors.includes('time_decay_exit')) modifier *= 0.9;   // Reduces loss by 10%
    return modifier;
  }

  getState(): AgentState {
    return {
      id: this.id,
      role: this.role,
      skillLevel: this.skillLevel,
      winRate: this.winRate + this.getWinRateModifier(),
      unlockedFactors: this.unlockedFactors,
      learningStatus: this.learningStatus,
      economicTracker: {
        balance: this.economicTracker.balance,
        tokenCosts: this.economicTracker.tokenCosts,
        tradeCosts: this.economicTracker.tradeCosts,
        realizedPnl: this.economicTracker.realizedPnl,
        status: this.economicTracker.getSurvivalStatus()
      }
    };
  }
}
