const TRUST_LEVELS = {
  bronze: {
    name: 'Bronze',
    emoji: '🥉',
    color: 0xcd7f32,
    requirements: {
      minMembers: 0,
      minDays: 0,
      minReputationScore: 0
    },
    benefits: {
      autoAction: 'alert',
      severityThreshold: 'critical',
      directoryListing: true,
      featuredEligible: false,
      partnershipLimit: 3
    }
  },
  silver: {
    name: 'Silver',
    emoji: '🥈',
    color: 0xc0c0c0,
    requirements: {
      minMembers: 100,
      minDays: 30,
      minReputationScore: 50
    },
    benefits: {
      autoAction: 'kick',
      severityThreshold: 'medium',
      directoryListing: true,
      featuredEligible: false,
      partnershipLimit: 5
    }
  },
  gold: {
    name: 'Gold',
    emoji: '🥇',
    color: 0xffd700,
    requirements: {
      minMembers: 500,
      minDays: 90,
      minReputationScore: 200
    },
    benefits: {
      autoAction: 'ban',
      severityThreshold: 'medium',
      directoryListing: true,
      featuredEligible: true,
      partnershipLimit: 10
    }
  },
  platinum: {
    name: 'Platinum',
    emoji: '💎',
    color: 0xe5e4e2,
    requirements: {
      minMembers: 1000,
      minDays: 180,
      minReputationScore: 500
    },
    benefits: {
      autoAction: 'ban',
      severityThreshold: 'low',
      directoryListing: true,
      featuredEligible: true,
      partnershipLimit: 20
    }
  }
};

const TRUST_ORDER = ['bronze', 'silver', 'gold', 'platinum'];

function calculateTrustLevel(serverData) {
  const memberCount = serverData.member_count || 0;
  const joinedAt = new Date(serverData.joined_federation_at || serverData.joined_at);
  const daysInFederation = Math.floor((Date.now() - joinedAt.getTime()) / (1000 * 60 * 60 * 24));
  const reputationScore = serverData.reputation_score || 0;
  
  let qualifiedLevel = 'bronze';
  
  for (const level of TRUST_ORDER) {
    const reqs = TRUST_LEVELS[level].requirements;
    if (memberCount >= reqs.minMembers && 
        daysInFederation >= reqs.minDays && 
        reputationScore >= reqs.minReputationScore) {
      qualifiedLevel = level;
    } else {
      break;
    }
  }
  
  return qualifiedLevel;
}

function getTrustLevelInfo(level) {
  return TRUST_LEVELS[level] || TRUST_LEVELS.bronze;
}

function getNextTrustLevel(currentLevel) {
  const currentIndex = TRUST_ORDER.indexOf(currentLevel);
  if (currentIndex === -1 || currentIndex >= TRUST_ORDER.length - 1) {
    return null;
  }
  return TRUST_ORDER[currentIndex + 1];
}

function getProgressToNextLevel(serverData) {
  const currentLevel = serverData.trust_level || 'bronze';
  const nextLevel = getNextTrustLevel(currentLevel);
  
  if (!nextLevel) {
    return { nextLevel: null, progress: {}, allMet: true };
  }
  
  const reqs = TRUST_LEVELS[nextLevel].requirements;
  const memberCount = serverData.member_count || 0;
  const joinedAt = new Date(serverData.joined_federation_at || serverData.joined_at);
  const daysInFederation = Math.floor((Date.now() - joinedAt.getTime()) / (1000 * 60 * 60 * 24));
  const reputationScore = serverData.reputation_score || 0;
  
  const progress = {
    members: {
      current: memberCount,
      required: reqs.minMembers,
      met: memberCount >= reqs.minMembers,
      percentage: Math.min(100, Math.floor((memberCount / reqs.minMembers) * 100)) || 0
    },
    days: {
      current: daysInFederation,
      required: reqs.minDays,
      met: daysInFederation >= reqs.minDays,
      percentage: Math.min(100, Math.floor((daysInFederation / reqs.minDays) * 100)) || 0
    },
    reputation: {
      current: reputationScore,
      required: reqs.minReputationScore,
      met: reputationScore >= reqs.minReputationScore,
      percentage: Math.min(100, Math.floor((reputationScore / reqs.minReputationScore) * 100)) || 0
    }
  };
  
  const allMet = progress.members.met && progress.days.met && progress.reputation.met;
  
  return { nextLevel, nextLevelInfo: TRUST_LEVELS[nextLevel], progress, allMet };
}

function shouldAutoAction(trustLevel, banSeverity, isPremium) {
  const levelInfo = getTrustLevelInfo(trustLevel);
  const severityOrder = ['low', 'medium', 'high', 'critical'];
  
  const banSeverityIndex = severityOrder.indexOf(banSeverity);
  const thresholdIndex = severityOrder.indexOf(levelInfo.benefits.severityThreshold);
  
  if (banSeverityIndex < thresholdIndex) {
    if (banSeverity === 'critical') {
      return { action: 'ban', reason: 'critical_severity' };
    }
    return { action: 'alert', reason: 'below_threshold' };
  }
  
  if (isPremium) {
    return { action: levelInfo.benefits.autoAction, reason: 'premium_protection' };
  }
  
  if (banSeverity === 'critical') {
    return { action: 'ban', reason: 'critical_severity' };
  }
  
  return { action: 'alert', reason: 'free_tier' };
}

function calculateReputationChange(action, successful = true) {
  const reputationValues = {
    ban_report: successful ? 10 : -5,
    report_verified: 15,
    false_positive: -20,
    event_hosted: 25,
    partnership_formed: 10,
    monthly_activity: 5
  };
  
  return reputationValues[action] || 0;
}

module.exports = {
  TRUST_LEVELS,
  TRUST_ORDER,
  calculateTrustLevel,
  getTrustLevelInfo,
  getNextTrustLevel,
  getProgressToNextLevel,
  shouldAutoAction,
  calculateReputationChange
};
