import React from 'react';

import { Database } from '@/packages/supabase/src/database.types';
import Isikukood from 'isikukood';
import { Clock, TrendingUp, User } from 'lucide-react';

import type { BmiThresholds } from '@kit/accounts/types/accounts';

import {
  bmiFromMetric,
  getBmiBackgroundColor,
  getBmiStatus,
} from '~/lib/utils';

import { TeamAccountStatisticsProps } from '../../_components/team-account-statistics';

interface AccountHealthDetailsField {
  title: string;
  value: string | number;
  Icon: React.ComponentType<{
    size?: number;
    color?: string;
    className?: string;
  }>;
  iconBg: string;
}

export const getAccountHealthDetailsFields = (
  memberParams: TeamAccountStatisticsProps['memberParams'],
  bmiThresholds: Omit<BmiThresholds, 'id'>[],
  members: Database['medreport']['Functions']['get_account_members']['Returns'],
): AccountHealthDetailsField[] => {
  const averageWeight =
    memberParams.reduce((sum, r) => sum + r.weight!, 0) / memberParams.length;
  const averageHeight =
    memberParams.reduce((sum, r) => sum + r.height!, 0) / memberParams.length;
  const averageAge =
    members.reduce((sum, r) => {
      const person = new Isikukood(r.personal_code);
      return sum + person.getAge();
    }, 0) / members.length;
  const numberOfMaleMembers = members.filter((r) => {
    const person = new Isikukood(r.personal_code);
    return person.getGender() === 'male';
  }).length;
  const numberOfFemaleMembers = members.filter((r) => {
    const person = new Isikukood(r.personal_code);
    return person.getGender() === 'female';
  }).length;
  const averageBMI = bmiFromMetric(averageWeight, averageHeight);
  const bmiStatus = getBmiStatus(bmiThresholds, {
    age: averageAge,
    height: averageHeight,
    weight: averageWeight,
  });
  const malePercentage = members.length
    ? (numberOfMaleMembers / members.length) * 100
    : 0;
  const femalePercentage = members.length
    ? (numberOfFemaleMembers / members.length) * 100
    : 0;

  return [
    {
      title: 'teams:healthDetails.women',
      value: `${femalePercentage.toFixed(0)}% (${numberOfFemaleMembers})`,
      Icon: User,
      iconBg: 'bg-success',
    },
    {
      title: 'teams:healthDetails.men',
      value: `${malePercentage.toFixed(0)}% (${numberOfMaleMembers})`,
      Icon: User,
      iconBg: 'bg-success',
    },
    {
      title: 'teams:healthDetails.avgAge',
      value: averageAge.toFixed(0),
      Icon: Clock,
      iconBg: 'bg-success',
    },
    {
      title: 'teams:healthDetails.bmi',
      value: averageBMI!,
      Icon: TrendingUp,
      iconBg: getBmiBackgroundColor(bmiStatus),
    },
    {
      title: 'teams:healthDetails.cholesterol',
      value: '-',
      Icon: TrendingUp,
      iconBg: 'bg-warning',
    },
    {
      title: 'teams:healthDetails.vitaminD',
      value: '-',
      Icon: TrendingUp,
      iconBg: 'bg-warning',
    },
    {
      title: 'teams:healthDetails.smokers',
      value: '-',
      Icon: TrendingUp,
      iconBg: 'bg-warning',
    },
  ];
};
