export type CreditCategory = 'Excellent' | 'Good' | 'Fair' | 'Poor';
export type Impact = 'High Impact' | 'Medium Impact' | 'Low Impact';

const getCategoryFromValue = (value: number, key: string): CreditCategory => {
  switch (key) {
    // Payment history (percentage of on-time payments)
    case 'payment_history':
      if (value >= 98) return 'Excellent';
      if (value >= 95) return 'Good';
      if (value >= 90) return 'Fair';
      return 'Poor';


    // Credit utilization (% of limit used)
    case 'utilization_percent':
      if (value <= 10) return 'Excellent';
      if (value <= 30) return 'Good';
      if (value <= 50) return 'Fair';
      return 'Poor';

    // Derogatory marks (collections, charge-offs, etc.)
    case 'derogatory_marks':
      if (value === 0) return 'Excellent';
      if (value <= 1) return 'Good';
      if (value <= 3) return 'Fair';
      return 'Poor';

    // Average age of credit (years)
    case 'ageofcredit':
      if (value >= 9) return 'Excellent';
      if (value >= 5) return 'Good';
      if (value >= 2) return 'Fair';
      return 'Poor';

    // Total number of accounts
    case 'total_accounts':
      if (value >= 20) return 'Excellent';
      if (value >= 10) return 'Good';
      if (value >= 5) return 'Fair';
      return 'Poor';

    // Hard inquiries in last 12 months
    case 'enquiry':
      if (value === 0) return 'Excellent';
      if (value <= 2) return 'Good';
      if (value <= 5) return 'Fair';
      return 'Poor';

    default:
      return 'Fair';
  }
};

export const mapReportScoreToCards = (api: any) => {
  const r = api.results;

  return [
    {
      title: 'Payment history',
      value: `${r.payment_history}%`,
      category: getCategoryFromValue(r.payment_history, 'payment_history'),
      impact: 'High Impact' as Impact,
      description: 'Consistency of on-time payments.',
    },
    {
      title: 'Credit card use',
      value: `${r.utilization_percent}%`,
      category: getCategoryFromValue(r.utilization_percent, 'utilization_percent'),
      impact: 'High Impact' as Impact,
      description: 'Low credit utilization improves your score.',
    },
    {
      title: 'Derogatory marks',
      value: `${r.derogatory_marks}`,
      category: getCategoryFromValue(r.derogatory_marks, 'derogatory_marks'),
      impact: 'High Impact' as Impact,
      description: 'Negative items on your credit report.',
    },
    {
      title: 'Credit age',
      value: `${r.ageofcredit} yrs`,
      category: getCategoryFromValue(r.ageofcredit, 'ageofcredit'),
      impact: 'Medium Impact' as Impact,
      description: 'Average age of your credit accounts.',
    },
    {
      title: 'Total accounts',
      value: `${r.total_accounts.total}`,
      category: getCategoryFromValue(r.total_accounts.total, 'total_accounts'),
      impact: 'Low Impact' as Impact,
      description: 'Total number of credit accounts.',
    },
    {
      title: 'Hard inquiries',
      value: `${r.enquiry}`,
      category: getCategoryFromValue(r.enquiry, 'enquiry'),
      impact: 'Low Impact' as Impact,
      description: 'Recent credit checks.',
    },
  ];
};
