All files / src/components/features/admin/analytics/CustomerInsights index.tsx

0% Statements 0/245
100% Branches 0/0
0% Functions 0/1
0% Lines 0/245

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
/**
 * CustomerInsights Component
 *
 * Displays customer segment breakdown with actionable insights.
 */

"use client";

import { Icon } from "@/components/ui/icons";
import { cn } from "@/lib/core";

export interface CustomerSegments {
  high: { count: number; revenue: number; avgOrderValue: number };
  medium: { count: number; revenue: number; avgOrderValue: number };
  low: { count: number; revenue: number; avgOrderValue: number };
  dormant: { count: number; lastOrderDaysAgo: number };
}

export interface CustomerInsightsProps {
  segments: CustomerSegments | null;
  isLoading?: boolean;
  className?: string;
}

function formatCurrency(value: number): string {
  return new Intl.NumberFormat("en-US", {
    style: "currency",
    currency: "USD",
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
  }).format(value);
}

interface SegmentCardProps {
  label: string;
  count: number;
  revenue: number;
  avgOrderValue: number;
  color: "green" | "blue" | "gray" | "yellow";
  icon: "star" | "user" | "users" | "alert-circle";
}

function SegmentCard({
  label,
  count,
  revenue,
  avgOrderValue,
  color,
  icon,
}: SegmentCardProps) {
  const colorClasses = {
    green: "bg-green-50 dark:bg-green-900/20 border-green-200 dark:border-green-800",
    blue: "bg-blue-50 dark:bg-blue-900/20 border-blue-200 dark:border-blue-800",
    gray: "bg-gray-50 dark:bg-gray-800 border-gray-200 dark:border-gray-700",
    yellow: "bg-yellow-50 dark:bg-yellow-900/20 border-yellow-200 dark:border-yellow-800",
  };

  const iconColorClasses = {
    green: "text-green-600 dark:text-green-400",
    blue: "text-blue-600 dark:text-blue-400",
    gray: "text-gray-600 dark:text-gray-400",
    yellow: "text-yellow-600 dark:text-yellow-400",
  };

  return (
    <div className={cn("rounded-lg border p-4", colorClasses[color])}>
      <div className="flex items-center gap-3">
        <div className={cn("p-2 rounded-lg bg-white dark:bg-gray-900", iconColorClasses[color])}>
          <Icon name={icon} className="h-5 w-5" />
        </div>
        <div>
          <h4 className="font-medium text-gray-900 dark:text-white">{label}</h4>
          <p className="text-2xl font-bold text-gray-900 dark:text-white">
            {count.toLocaleString()}
          </p>
        </div>
      </div>

      <div className="mt-4 grid grid-cols-2 gap-4">
        <div>
          <p className="text-xs text-gray-500 dark:text-gray-400">Total Revenue</p>
          <p className="text-sm font-semibold text-gray-900 dark:text-white">
            {formatCurrency(revenue)}
          </p>
        </div>
        <div>
          <p className="text-xs text-gray-500 dark:text-gray-400">Avg Order Value</p>
          <p className="text-sm font-semibold text-gray-900 dark:text-white">
            {formatCurrency(avgOrderValue)}
          </p>
        </div>
      </div>
    </div>
  );
}

export function CustomerInsights({
  segments,
  isLoading,
  className,
}: CustomerInsightsProps) {
  if (isLoading) {
    return (
      <div className={cn("bg-white dark:bg-gray-900 rounded-lg border border-gray-200 dark:border-gray-800 p-6", className)}>
        <div className="animate-pulse space-y-4">
          <div className="h-6 bg-gray-200 dark:bg-gray-700 rounded w-1/4"></div>
          <div className="grid grid-cols-2 lg:grid-cols-4 gap-4">
            {[...Array(4)].map((_, i) => (
              <div key={i} className="h-32 bg-gray-200 dark:bg-gray-700 rounded"></div>
            ))}
          </div>
        </div>
      </div>
    );
  }

  if (!segments) {
    return (
      <div className={cn("bg-white dark:bg-gray-900 rounded-lg border border-gray-200 dark:border-gray-800 p-6", className)}>
        <h3 className="text-lg font-semibold text-gray-900 dark:text-white mb-4">
          Customer Insights
        </h3>
        <p className="text-gray-500 dark:text-gray-400 text-center py-8">
          No customer data available
        </p>
      </div>
    );
  }

  const totalCustomers =
    segments.high.count + segments.medium.count + segments.low.count + segments.dormant.count;
  const totalRevenue = segments.high.revenue + segments.medium.revenue + segments.low.revenue;
  const highValuePercentage =
    totalCustomers > 0 ? ((segments.high.count / totalCustomers) * 100).toFixed(1) : "0";
  const highRevenuePercentage =
    totalRevenue > 0 ? ((segments.high.revenue / totalRevenue) * 100).toFixed(0) : "0";

  return (
    <div className={cn("bg-white dark:bg-gray-900 rounded-lg border border-gray-200 dark:border-gray-800 overflow-hidden", className)}>
      <div className="p-6 border-b border-gray-200 dark:border-gray-800">
        <h3 className="text-lg font-semibold text-gray-900 dark:text-white">
          Customer Insights
        </h3>
        <p className="mt-1 text-sm text-gray-500 dark:text-gray-400">
          Segmented by purchase value over time
        </p>
      </div>

      <div className="p-6">
        <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
          <SegmentCard
            label="High Value"
            count={segments.high.count}
            revenue={segments.high.revenue}
            avgOrderValue={segments.high.avgOrderValue}
            color="green"
            icon="star"
          />
          <SegmentCard
            label="Medium Value"
            count={segments.medium.count}
            revenue={segments.medium.revenue}
            avgOrderValue={segments.medium.avgOrderValue}
            color="blue"
            icon="user"
          />
          <SegmentCard
            label="Low Value"
            count={segments.low.count}
            revenue={segments.low.revenue}
            avgOrderValue={segments.low.avgOrderValue}
            color="gray"
            icon="users"
          />
          <div className="rounded-lg border border-yellow-200 dark:border-yellow-800 bg-yellow-50 dark:bg-yellow-900/20 p-4">
            <div className="flex items-center gap-3">
              <div className="p-2 rounded-lg bg-white dark:bg-gray-900 text-yellow-600 dark:text-yellow-400">
                <Icon name="alert-circle" className="h-5 w-5" />
              </div>
              <div>
                <h4 className="font-medium text-gray-900 dark:text-white">Dormant</h4>
                <p className="text-2xl font-bold text-gray-900 dark:text-white">
                  {segments.dormant.count.toLocaleString()}
                </p>
              </div>
            </div>

            <div className="mt-4">
              <p className="text-xs text-gray-500 dark:text-gray-400">
                Avg. days since last order
              </p>
              <p className="text-sm font-semibold text-yellow-600 dark:text-yellow-400">
                {segments.dormant.lastOrderDaysAgo}+ days
              </p>
            </div>
          </div>
        </div>

        {/* Insights Summary */}
        <div className="mt-6 p-4 bg-gray-50 dark:bg-gray-800 rounded-lg">
          <h4 className="text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
            Key Insights
          </h4>
          <ul className="space-y-2 text-sm text-gray-600 dark:text-gray-400">
            <li className="flex items-start gap-2">
              <Icon name="check" className="h-4 w-4 text-green-500 mt-0.5 flex-shrink-0" />
              <span>
                <strong className="text-gray-900 dark:text-white">{highValuePercentage}%</strong> of
                customers are high value, generating{" "}
                <strong className="text-gray-900 dark:text-white">{highRevenuePercentage}%</strong>{" "}
                of revenue
              </span>
            </li>
            {segments.dormant.count > 0 && (
              <li className="flex items-start gap-2">
                <Icon name="alert-circle" className="h-4 w-4 text-yellow-500 mt-0.5 flex-shrink-0" />
                <span>
                  <strong className="text-gray-900 dark:text-white">
                    {segments.dormant.count.toLocaleString()}
                  </strong>{" "}
                  customers haven&apos;t ordered in {segments.dormant.lastOrderDaysAgo}+ days.
                  Consider a re-engagement campaign.
                </span>
              </li>
            )}
            {segments.low.count > segments.high.count * 2 && (
              <li className="flex items-start gap-2">
                <Icon name="trending-up" className="h-4 w-4 text-blue-500 mt-0.5 flex-shrink-0" />
                <span>
                  Opportunity: Upgrade{" "}
                  <strong className="text-gray-900 dark:text-white">
                    {segments.low.count.toLocaleString()}
                  </strong>{" "}
                  low-value customers with targeted promotions
                </span>
              </li>
            )}
          </ul>
        </div>
      </div>
    </div>
  );
}

export default CustomerInsights;