import React from 'react'
import { FiMinus } from 'react-icons/fi'
import { GoArrowDownLeft, GoArrowUpRight } from 'react-icons/go'

import { PerformanceDelta } from '../types'

interface PerformanceKpiCardProps {
  title: string
  value: string | number
  context?: string
  delta?: PerformanceDelta | null
  icon?: React.ReactNode
}

export default function PerformanceKpiCard({
  title,
  value,
  context,
  delta,
  icon,
}: PerformanceKpiCardProps) {
  const improved = delta?.trend === 'improved'
  const declined = delta?.trend === 'declined'

  return (
    <div className="rounded-xl border border-slate-200 bg-white p-5 shadow-sm print:shadow-none">
      <div className="flex items-center justify-between gap-3">
        <p className="text-sm font-medium text-slate-500">{title}</p>
        {icon}
      </div>

      <p className="mt-3 text-3xl font-bold tabular-nums text-slate-900">
        {value}
      </p>

      {delta && delta.percent !== null && (
        <div className="mt-3 flex flex-wrap items-center gap-2 text-sm">
          <span
            className={`flex items-center gap-1 font-semibold ${
              improved
                ? 'text-green-600'
                : declined
                  ? 'text-red-600'
                  : 'text-slate-500'
            }`}
          >
            {improved ? (
              <GoArrowUpRight className="h-4 w-4" />
            ) : declined ? (
              <GoArrowDownLeft className="h-4 w-4" />
            ) : (
              <FiMinus className="h-4 w-4" />
            )}
            {delta.percent > 0 ? '+' : ''}${delta.percent}%
          </span>
          <span className="text-slate-400">vs previous period</span>
        </div>
      )}

      {context && <p className="mt-2 text-sm text-slate-500">{context}</p>}
    </div>
  )
}
