All files / src/app/admin/referrals page.tsx

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

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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
"use client";

import { useState, useEffect } from "react";
import { clientLogger } from "@/lib/logging/clientLogger";
import Link from "next/link";
import { Icon } from "@/components/ui/icons";
import { Button } from "@/components/ui/Button";

interface ReferralStats {
  totalReferrals: number;
  successfulReferrals: number;
  pendingReferrals: number;
  totalRewardsIssued: number;
  conversionRate: number;
  topReferrers: {
    id: number;
    name: string;
    email: string;
    referralCount: number;
    rewardsEarned: number;
  }[];
}

interface ReferralConfig {
  referrerReward: number;
  refereeReward: number;
  minPurchaseAmount: number;
  expirationDays: number;
  maxReferralsPerUser: number | null;
}

export default function ReferralsManagementPage() {
  const [stats, setStats] = useState<ReferralStats | null>(null);
  const [config, setConfig] = useState<ReferralConfig | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    fetchData();
  }, []);

  const fetchData = async () => {
    try {
      setLoading(true);
      const [statsRes, configRes] = await Promise.all([
        fetch("/api/admin/referrals/analytics"),
        fetch("/api/admin/referrals/config"),
      ]);

      if (statsRes.ok) {
        const statsData = await statsRes.json();
        setStats(statsData);
      }

      if (configRes.ok) {
        const configData = await configRes.json();
        setConfig(configData);
      }
    } catch (err) {
      setError("Failed to load referral data");
      clientLogger.error("Failed to load referral data", err instanceof Error ? err : undefined);
    } finally {
      setLoading(false);
    }
  };

  if (loading) {
    return (
      <div className="p-6">
        <div className="animate-pulse space-y-4">
          <div className="h-8 bg-gray-200 dark:bg-gray-700 rounded w-1/4"></div>
          <div className="grid grid-cols-1 md:grid-cols-4 gap-4">
            {[...Array(4)].map((_, i) => (
              <div key={i} className="h-24 bg-gray-200 dark:bg-gray-700 rounded"></div>
            ))}
          </div>
        </div>
      </div>
    );
  }

  return (
    <div className="p-6 space-y-6">
      {/* Header */}
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-bold text-gray-900 dark:text-gray-100">Referral Program</h1>
          <p className="text-gray-600 dark:text-gray-400 mt-1">
            Manage referral rewards, track conversions, and view top referrers
          </p>
        </div>
        <div className="flex gap-3">
          <Link href="/admin/referrals/settings">
            <Button variant="outline">
              <Icon name="edit" className="w-4 h-4 mr-2" />
              Settings
            </Button>
          </Link>
          <Link href="/admin/referrals/campaigns">
            <Button variant="primary">
              <Icon name="plus" className="w-4 h-4 mr-2" />
              New Campaign
            </Button>
          </Link>
        </div>
      </div>

      {error && (
        <div className="bg-red-50 dark:bg-red-900/30 border border-red-200 dark:border-red-800 text-red-700 dark:text-red-300 px-4 py-3 rounded-lg">
          {error}
        </div>
      )}

      {/* Stats Cards */}
      <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
        <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4">
          <div className="flex items-center gap-3">
            <div className="p-2 bg-blue-100 dark:bg-blue-900/30 rounded-lg">
              <Icon name="user" className="w-5 h-5 text-blue-600 dark:text-blue-400" />
            </div>
            <div>
              <p className="text-sm text-gray-600 dark:text-gray-400">Total Referrals</p>
              <p className="text-2xl font-bold dark:text-gray-100">
                {stats?.totalReferrals?.toLocaleString() || 0}
              </p>
            </div>
          </div>
        </div>

        <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4">
          <div className="flex items-center gap-3">
            <div className="p-2 bg-green-100 dark:bg-green-900/30 rounded-lg">
              <Icon name="check" className="w-5 h-5 text-green-600 dark:text-green-400" />
            </div>
            <div>
              <p className="text-sm text-gray-600 dark:text-gray-400">Successful</p>
              <p className="text-2xl font-bold dark:text-gray-100">
                {stats?.successfulReferrals?.toLocaleString() || 0}
              </p>
            </div>
          </div>
        </div>

        <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4">
          <div className="flex items-center gap-3">
            <div className="p-2 bg-yellow-100 dark:bg-yellow-900/30 rounded-lg">
              <Icon name="clock" className="w-5 h-5 text-yellow-600 dark:text-yellow-400" />
            </div>
            <div>
              <p className="text-sm text-gray-600 dark:text-gray-400">Pending</p>
              <p className="text-2xl font-bold dark:text-gray-100">
                {stats?.pendingReferrals?.toLocaleString() || 0}
              </p>
            </div>
          </div>
        </div>

        <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4">
          <div className="flex items-center gap-3">
            <div className="p-2 bg-purple-100 dark:bg-purple-900/30 rounded-lg">
              <Icon name="arrow" className="w-5 h-5 text-purple-600 dark:text-purple-400" />
            </div>
            <div>
              <p className="text-sm text-gray-600 dark:text-gray-400">Conversion Rate</p>
              <p className="text-2xl font-bold dark:text-gray-100">
                {(stats?.conversionRate || 0).toFixed(1)}%
              </p>
            </div>
          </div>
        </div>
      </div>

      {/* Program Configuration */}
      {config && (
        <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700">
          <div className="p-4 border-b border-gray-200 dark:border-gray-700 flex items-center justify-between">
            <h2 className="text-lg font-semibold dark:text-gray-100">Program Configuration</h2>
            <Link href="/admin/referrals/settings">
              <Button variant="ghost" size="sm">
                <Icon name="edit" className="w-4 h-4 mr-1" />
                Edit
              </Button>
            </Link>
          </div>
          <div className="p-4 grid grid-cols-2 md:grid-cols-5 gap-4">
            <div>
              <p className="text-sm text-gray-600 dark:text-gray-400">Referrer Reward</p>
              <p className="font-semibold dark:text-gray-100">${config.referrerReward}</p>
            </div>
            <div>
              <p className="text-sm text-gray-600 dark:text-gray-400">Referee Reward</p>
              <p className="font-semibold dark:text-gray-100">${config.refereeReward}</p>
            </div>
            <div>
              <p className="text-sm text-gray-600 dark:text-gray-400">Min Purchase</p>
              <p className="font-semibold dark:text-gray-100">${config.minPurchaseAmount}</p>
            </div>
            <div>
              <p className="text-sm text-gray-600 dark:text-gray-400">Expiration</p>
              <p className="font-semibold dark:text-gray-100">{config.expirationDays} days</p>
            </div>
            <div>
              <p className="text-sm text-gray-600 dark:text-gray-400">Max Referrals</p>
              <p className="font-semibold dark:text-gray-100">
                {config.maxReferralsPerUser || "Unlimited"}
              </p>
            </div>
          </div>
        </div>
      )}

      {/* Top Referrers */}
      <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700">
        <div className="p-4 border-b border-gray-200 dark:border-gray-700 flex items-center justify-between">
          <h2 className="text-lg font-semibold dark:text-gray-100">Top Referrers</h2>
          <Link href="/admin/referrals/leaderboard">
            <Button variant="ghost" size="sm">
              View All
              <Icon name="chevron-right" className="w-4 h-4 ml-1" />
            </Button>
          </Link>
        </div>
        <div className="overflow-x-auto">
          {stats?.topReferrers && stats.topReferrers.length > 0 ? (
            <table className="w-full">
              <thead className="bg-gray-50 dark:bg-gray-700">
                <tr>
                  <th className="px-4 py-3 text-left text-sm font-medium text-gray-600 dark:text-gray-400">
                    Rank
                  </th>
                  <th className="px-4 py-3 text-left text-sm font-medium text-gray-600 dark:text-gray-400">
                    User
                  </th>
                  <th className="px-4 py-3 text-right text-sm font-medium text-gray-600 dark:text-gray-400">
                    Referrals
                  </th>
                  <th className="px-4 py-3 text-right text-sm font-medium text-gray-600 dark:text-gray-400">
                    Rewards Earned
                  </th>
                </tr>
              </thead>
              <tbody className="divide-y divide-gray-200 dark:divide-gray-700">
                {stats.topReferrers.map((referrer, index) => (
                  <tr key={referrer.id} className="hover:bg-gray-50 dark:hover:bg-gray-700">
                    <td className="px-4 py-3">
                      <span
                        className={`inline-flex items-center justify-center w-6 h-6 rounded-full text-sm font-medium ${
                          index === 0
                            ? "bg-yellow-100 dark:bg-yellow-900/30 text-yellow-700 dark:text-yellow-400"
                            : index === 1
                            ? "bg-gray-100 dark:bg-gray-600 text-gray-700 dark:text-gray-300"
                            : index === 2
                            ? "bg-orange-100 dark:bg-orange-900/30 text-orange-700 dark:text-orange-400"
                            : "bg-gray-50 dark:bg-gray-700 text-gray-500 dark:text-gray-400"
                        }`}
                      >
                        {index + 1}
                      </span>
                    </td>
                    <td className="px-4 py-3">
                      <div>
                        <p className="font-medium dark:text-gray-100">{referrer.name}</p>
                        <p className="text-sm text-gray-500 dark:text-gray-400">{referrer.email}</p>
                      </div>
                    </td>
                    <td className="px-4 py-3 text-right font-medium dark:text-gray-100">
                      {referrer.referralCount}
                    </td>
                    <td className="px-4 py-3 text-right font-medium text-green-600 dark:text-green-400">
                      ${referrer.rewardsEarned.toFixed(2)}
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          ) : (
            <div className="text-center py-8 text-gray-500 dark:text-gray-400">
              <Icon name="user" className="w-12 h-12 mx-auto mb-3 text-gray-300 dark:text-gray-600" />
              <p>No referrals yet</p>
              <p className="text-sm mt-1">
                Referrals will appear here once customers start sharing
              </p>
            </div>
          )}
        </div>
      </div>

      {/* Quick Actions */}
      <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700">
        <div className="p-4 border-b border-gray-200 dark:border-gray-700">
          <h2 className="text-lg font-semibold dark:text-gray-100">Quick Actions</h2>
        </div>
        <div className="p-4 grid grid-cols-1 md:grid-cols-3 gap-4">
          <Link
            href="/admin/referrals/codes"
            className="flex items-center gap-3 p-3 border border-gray-200 dark:border-gray-700 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors"
          >
            <Icon name="tag" className="w-5 h-5 text-gray-600 dark:text-gray-400" />
            <div>
              <p className="font-medium dark:text-gray-100">Manage Codes</p>
              <p className="text-sm text-gray-500 dark:text-gray-400">View and create referral codes</p>
            </div>
          </Link>
          <Link
            href="/admin/referrals/rewards"
            className="flex items-center gap-3 p-3 border border-gray-200 dark:border-gray-700 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors"
          >
            <Icon name="star" className="w-5 h-5 text-gray-600 dark:text-gray-400" />
            <div>
              <p className="font-medium dark:text-gray-100">Reward History</p>
              <p className="text-sm text-gray-500 dark:text-gray-400">View issued rewards</p>
            </div>
          </Link>
          <Link
            href="/admin/referrals/reports"
            className="flex items-center gap-3 p-3 border border-gray-200 dark:border-gray-700 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors"
          >
            <Icon name="dashboard" className="w-5 h-5 text-gray-600 dark:text-gray-400" />
            <div>
              <p className="font-medium dark:text-gray-100">Analytics Reports</p>
              <p className="text-sm text-gray-500 dark:text-gray-400">Detailed referral insights</p>
            </div>
          </Link>
        </div>
      </div>
    </div>
  );
}