'use client';
import { useMemo } from 'react';
import { motion } from 'framer-motion';
import { clsx } from 'clsx';
import {
Users, CalendarPlus, Briefcase, TrendingUp, Clock, Zap,
CheckCircle2, Circle, AlertTriangle, MessageSquare, Phone,
ArrowUpRight, Sparkles, ChevronLeft, ChevronRight,
FileText, InboxIcon,
} from 'lucide-react';
import { useI18n } from '@/i18n';
import { KpiCard } from '@/components/ui/kpi-card';
import { EmptyState } from '@/components/ui/empty-state';
/* ---------- Types ---------- */
interface Task {
id: string;
title: string;
dueStatus: 'overdue' | 'today' | 'upcoming';
time?: string;
}
interface Deal {
id: string;
name: string;
value: number;
stage: string;
stageColor: string;
}
interface Activity {
id: string;
type: 'message' | 'call' | 'dealUpdate' | 'noteAdded';
text: string;
time: string;
}
interface AiInsight {
id: string;
type: 'followUp' | 'closing' | 'risk';
count: number;
}
interface SalesWorkspaceProps {
userName?: string;
kpis?: {
totalLeads: number;
newToday: number;
openDeals: number;
wonValue: number;
conversionRate: number;
responseTime: number;
};
tasks?: Task[];
deals?: Deal[];
activities?: Activity[];
insights?: AiInsight[];
className?: string;
}
/* ---------- Demo data ---------- */
const demoKpis = {
totalLeads: 1247,
newToday: 18,
openDeals: 43,
wonValue: 892500,
conversionRate: 34,
responseTime: 12,
};
const demoTasks: Task[] = [
{ id: '1', title: 'متابعة أحمد الشمري — عرض عقار', dueStatus: 'overdue', time: 'أمس' },
{ id: '2', title: 'اتصال مع نورة — عرض سعر', dueStatus: 'today', time: '2:00 م' },
{ id: '3', title: 'إرسال عقد لشركة المستقبل', dueStatus: 'today', time: '4:30 م' },
{ id: '4', title: 'جدولة عرض تقديمي', dueStatus: 'upcoming', time: 'غداً' },
];
const demoDeals: Deal[] = [
{ id: '1', name: 'صفقة أبراج الرياض', value: 2500000, stage: 'تفاوض', stageColor: 'bg-amber-500' },
{ id: '2', name: 'مشروع المجمع التجاري', value: 1800000, stage: 'عرض سعر', stageColor: 'bg-teal-500' },
{ id: '3', name: 'فيلا حي النرجس', value: 950000, stage: 'مؤهّل', stageColor: 'bg-blue-500' },
{ id: '4', name: 'مكاتب طريق الملك', value: 780000, stage: 'تفاوض', stageColor: 'bg-amber-500' },
{ id: '5', name: 'شقق حي الملقا', value: 650000, stage: 'عرض سعر', stageColor: 'bg-teal-500' },
];
const demoActivities: Activity[] = [
{ id: '1', type: 'message', text: 'رسالة من أحمد: "ابي تفاصيل العرض"', time: 'منذ 5 دقائق' },
{ id: '2', type: 'call', text: 'مكالمة مع نورة — 8 دقائق', time: 'منذ 30 دقيقة' },
{ id: '3', type: 'dealUpdate', text: 'صفقة أبراج الرياض انتقلت لمرحلة التفاوض', time: 'منذ ساعة' },
{ id: '4', type: 'noteAdded', text: 'ملاحظة على فيلا النرجس: العميل يبي جراج إضافي', time: 'منذ 2 ساعة' },
];
const demoInsights: AiInsight[] = [
{ id: '1', type: 'followUp', count: 3 },
{ id: '2', type: 'closing', count: 2 },
{ id: '3', type: 'risk', count: 1 },
];
/* ---------- Sub-components ---------- */
const stagger = {
hidden: {},
visible: { transition: { staggerChildren: 0.06 } },
};
const fadeUp = {
hidden: { opacity: 0, y: 12 },
visible: { opacity: 1, y: 0, transition: { duration: 0.35 } },
};
function GlassCard({ children, className }: { children: React.ReactNode; className?: string }) {
return (
{children}
);
}
function SectionHeader({ icon: Icon, title }: { icon: typeof Users; title: string }) {
return (
{title}
);
}
const activityIcons: Record = {
message: MessageSquare,
call: Phone,
dealUpdate: ArrowUpRight,
noteAdded: FileText,
};
const taskStatusStyles: Record = {
overdue: { dot: 'bg-rose-500', text: 'text-rose-400' },
today: { dot: 'bg-amber-500', text: 'text-amber-400' },
upcoming: { dot: 'bg-slate-500', text: 'text-slate-400' },
};
const insightIcons: Record = {
followUp: { icon: Clock, color: 'text-amber-400' },
closing: { icon: TrendingUp, color: 'text-emerald-400' },
risk: { icon: AlertTriangle, color: 'text-rose-400' },
};
/* ---------- Main ---------- */
function SalesWorkspace({
userName,
kpis: kpisProp,
tasks: tasksProp,
deals: dealsProp,
activities: activitiesProp,
insights: insightsProp,
className,
}: SalesWorkspaceProps) {
const { t, dir, locale, isArabic } = useI18n();
const kpis = kpisProp ?? demoKpis;
const tasks = tasksProp ?? demoTasks;
const deals = dealsProp ?? demoDeals;
const activities = activitiesProp ?? demoActivities;
const insights = insightsProp ?? demoInsights;
const greeting = useMemo(() => {
const hour = new Date().getHours();
const base = hour < 17 ? t('workspace.greeting') : t('workspace.greetingEvening');
return userName ? `${base}، ${userName}` : base;
}, [t, userName]);
const formatCurrency = (val: number) =>
new Intl.NumberFormat(locale === 'ar' ? 'ar-SA' : 'en-US', {
style: 'currency',
currency: 'SAR',
maximumFractionDigits: 0,
}).format(val);
const kpiDefs = [
{ key: 'totalLeads', value: kpis.totalLeads, label: t('dashboard.kpis.totalLeads'), icon: Users, trend: { direction: 'up' as const, percentage: 12 }, sparkline: [30, 42, 38, 55, 52, 68, 62] },
{ key: 'newToday', value: kpis.newToday, label: t('dashboard.kpis.newToday'), icon: CalendarPlus, trend: { direction: 'up' as const, percentage: 8 }, sparkline: [5, 8, 12, 9, 15, 11, 18] },
{ key: 'openDeals', value: kpis.openDeals, label: t('dashboard.kpis.openDeals'), icon: Briefcase, trend: { direction: 'up' as const, percentage: 5 }, sparkline: [28, 35, 31, 40, 38, 42, 43] },
{ key: 'wonValue', value: kpis.wonValue, label: t('dashboard.kpis.wonValue'), prefix: isArabic ? 'ر.س' : 'SAR', trend: { direction: 'up' as const, percentage: 22 }, sparkline: [400, 520, 480, 650, 720, 810, 892] },
{ key: 'conversionRate', value: kpis.conversionRate, label: t('dashboard.kpis.conversionRate'), suffix: '%', trend: { direction: 'down' as const, percentage: 3 }, sparkline: [38, 36, 35, 37, 34, 33, 34] },
{ key: 'responseTime', value: kpis.responseTime, label: t('dashboard.kpis.responseTime'), suffix: t('workspace.kpiResponseUnit'), trend: { direction: 'up' as const, percentage: 15 }, sparkline: [20, 18, 15, 14, 13, 12, 12] },
];
const insightLabel = (i: AiInsight) => {
const labels: Record = {
followUp: t('workspace.aiInsightFollowUp'),
closing: t('workspace.aiInsightClosing'),
risk: t('workspace.aiInsightRisk'),
};
return `${i.count} ${labels[i.type]}`;
};
return (
{/* Greeting */}
{greeting}
{/* KPI Bar */}
{kpiDefs.map((k) => (
))}
{/* 3-column body */}
{/* LEFT: Tasks */}
{tasks.length === 0 ? (
) : (
{tasks.map((task) => {
const style = taskStatusStyles[task.dueStatus];
return (
-
);
})}
)}
{/* CENTER: Hot Deals */}
{deals.length === 0 ? (
) : (
{deals.map((deal, idx) => (
{idx + 1}
{formatCurrency(deal.value)}
))}
)}
{/* RIGHT: Activity */}
{activities.length === 0 ? (
) : (
{activities.map((act) => {
const Icon = activityIcons[act.type];
return (
-
);
})}
)}
{/* AI Insights */}
{insights.map((insight) => {
const { icon: Icon, color } = insightIcons[insight.type];
return (
{insightLabel(insight)}
{isArabic ? (
) : (
)}
);
})}
);
}
export { SalesWorkspace };
export type { SalesWorkspaceProps, Task, Deal, Activity, AiInsight };