/**
 * Portfolio Color Palette System
 * Ported from frontend src/utils/portfolioColors.ts
 */

export const portfolioColorPalette = [
  '#3B82F6', // Blue 500
  '#06B6D4', // Cyan 500
  '#10B981', // Green 500
  '#14B8A6', // Teal 500
  '#8B5CF6', // Violet 500
  '#6366F1', // Indigo 500
  '#0EA5E9', // Sky 500
  '#22C55E', // Green 400
  '#84CC16', // Lime 500
  '#F59E0B', // Amber 500
  '#EC4899', // Pink 500
  '#EF4444', // Red 500
];

export function getProjectColor(index) {
  return portfolioColorPalette[index % portfolioColorPalette.length];
}

/**
 * Add colors and formatted percentages to portfolio projects
 * @param {Array} projects - Portfolio projects from Wren API
 * @returns {Array} Projects with color and percentage added
 */
export function formatPortfolioProjects(projects) {
  if (!projects || !Array.isArray(projects)) {
    return [];
  }

  return projects.map((project, index) => {
    // Wren API returns percentage as decimal (0.0025 = 0.25%)
    // Convert to percentage and round to 2 decimal places
    let percentageValue;
    if (project.percentage) {
      percentageValue = (project.percentage * 100).toFixed(2);
    } else {
      percentageValue = ((1 / projects.length) * 100).toFixed(2);
    }

    return {
      name: project.name || 'Unnamed Project',
      type: project.type || 'Carbon Offset',
      percentage: percentageValue,
      color: getProjectColor(index),
    };
  });
}
