Spaces:
Running

File size: 11,547 Bytes
d42fc97
98b04ca
50a005c
4655eeb
18afd52
c7ed040
d128011
50a005c
 
d3a69df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50a005c
 
 
 
 
 
 
 
 
d3a69df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98b04ca
 
 
 
 
 
 
 
 
 
 
d3a69df
 
 
 
 
 
 
 
 
 
 
c7ed040
d3a69df
 
 
c7ed040
d3a69df
 
 
 
 
 
6165987
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ed040
6165987
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50a005c
 
 
 
 
c7df2b5
 
50a005c
 
 
 
6165987
d128011
98b04ca
d128011
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50a005c
 
4655eeb
 
 
d4d279d
4655eeb
 
 
 
 
98b04ca
 
 
 
d128011
98b04ca
 
 
 
 
d128011
98b04ca
 
 
50a005c
4655eeb
 
50a005c
4655eeb
d3a69df
4655eeb
 
 
 
 
 
50a005c
 
 
 
4655eeb
 
 
 
 
 
 
50a005c
4655eeb
 
 
 
 
 
 
 
 
 
 
6165987
4655eeb
 
 
 
 
 
 
 
6165987
4655eeb
 
 
 
 
 
6165987
50a005c
 
4655eeb
 
 
 
 
 
d3a69df
4655eeb
 
 
 
 
 
50a005c
 
 
4655eeb
 
27faa4d
 
 
 
 
4655eeb
 
 
50a005c
 
4655eeb
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
import { BadgeCheck, ChevronRight, Clock, ImageOff, Star, Users, Zap } from "lucide-react";
import { useState } from "react";
import { Link } from "react-router";
import { TiltCard } from "@/components/ui/motion";
import { useTranslation } from "@/hooks/useTranslation";
import { formatNumber } from "@/lib/format";
import { youTubeThumbnailFallback } from "@/lib/youtube-url";

interface CourseCardProps {
  readonly course: {
    readonly id: number;
    readonly slug: string;
    readonly titleEn: string;
    readonly titleAr: string;
    readonly shortDescEn: string | null;
    readonly shortDescAr: string | null;
    readonly thumbnail: string | null;
    readonly level: string;
    readonly isPremium: boolean;
    readonly price: string;
    readonly originalPrice: string | null;
    readonly durationHours: number;
    readonly rating: string;
    readonly reviewCount: number;
    readonly studentCount: number;
    readonly categoryName?: string | null;
    readonly categoryNameAr?: string | null;
  };
}

const levelColors: Record<string, { bg: string; text: string; label: string; labelAr: string }> = {
  beginner:     { bg: "rgba(16,185,129,0.12)",  text: "#10b981", label: "Beginner",     labelAr: "مبتدئ" },
  intermediate: { bg: "rgba(245,158,11,0.12)",  text: "#f59e0b", label: "Intermediate", labelAr: "متوسط" },
  advanced:     { bg: "rgba(239,68,68,0.12)",   text: "#ef4444", label: "Advanced",     labelAr: "متقدم" },
};

function CourseCardBadges({ course, lang, discount }: { readonly course: { readonly isPremium: boolean }; readonly lang: "ar" | "en"; readonly discount: number }) {
  return (
    <div className="absolute start-3 top-3 flex items-center gap-2">
      {course.isPremium ? (
        <span className="flex items-center gap-1 rounded-lg bg-[rgba(6,182,212,0.9)] px-2.5 py-1 text-[11px] font-bold uppercase tracking-wider text-[#001a24] backdrop-blur-sm">
          <Zap className="h-3 w-3" />
          Premium
        </span>
      ) : (
        <span className="flex items-center gap-1 rounded-lg border border-[#f59e0b] bg-[rgba(245,158,11,0.15)] px-2.5 py-1 text-[11px] font-bold uppercase tracking-wider text-[#f59e0b] backdrop-blur-sm">
          {lang === "ar" ? "مجاني" : "Free"}
        </span>
      )}
      {discount > 0 && (
        <span className="rounded-lg bg-[rgba(239,68,68,0.85)] px-2.5 py-1 text-[11px] font-bold text-white backdrop-blur-sm">
          -{discount}%
        </span>
      )}
    </div>
  );
}

function ThumbnailFallback({ title }: { readonly title: string }) {
  return (
    <div className="flex h-full w-full items-center justify-center bg-gradient-to-br from-[#0a1628] to-[#1e2d3d]">
      <div className="text-center">
        <ImageOff className="mx-auto h-8 w-8 text-[#475569]" />
        <p className="mt-2 text-xs text-[#475569]">{title}</p>
      </div>
    </div>
  );
}

function CourseCardPrice({ coursePrice, originalPrice, lang }: { readonly coursePrice: number; readonly originalPrice: number; readonly lang: "ar" | "en" }) {
  if (coursePrice === 0) {
    return (
      <span className="text-base font-bold text-[#10b981]">
        {lang === "ar" ? "مجاناً" : "Free"}
      </span>
    );
  }
  return (
    <div className="flex items-baseline gap-2">
      <span className="text-xl font-extrabold text-[#e8f0fe]">
        {formatNumber(coursePrice)} {lang === "ar" ? "ج.م" : "EGP"}
      </span>
      {originalPrice > coursePrice && (
        <span className="text-sm text-[#475569] line-through">
          {formatNumber(originalPrice)}
        </span>
      )}
    </div>
  );
}

function CourseCardStats({ lang, rating, reviewCount, studentCount }: { readonly lang: "ar" | "en"; readonly rating: string; readonly reviewCount: number; readonly studentCount: number }) {
  return (
    <div className="mt-4 flex items-center gap-4">
      <div className="flex items-center gap-1">
        <Star className="h-3.5 w-3.5 fill-[#f59e0b] text-[#f59e0b]" />
        <span className="text-[12px] font-semibold text-[#e8f0fe]">
          {Number.parseFloat(rating).toFixed(1)}
        </span>
        <span className="text-[11px] text-[#475569]">
          ({reviewCount})
        </span>
      </div>
      <div className="h-3 w-px bg-[#1e2d3d]" />
      <div className="flex items-center gap-1">
        <Users className="h-3.5 w-3.5 text-[#475569]" />
        <span className="text-[11px] text-[#64748b]">
          {studentCount >= 1000
            ? `${(studentCount / 1000).toFixed(1)}k`
            : formatNumber(studentCount)}
          {lang === "ar" ? " طالب" : " students"}
        </span>
      </div>
      {/* Brand Kit §7 proof system — the ONE green check badge,
          identical language on course cards, reviews and
          certificates. Truthful platform-wide: completion of every
          course awards a verifiable certificate (QR /verify). */}
      <div className="flex items-center gap-1" title={lang === "ar" ? "شهادة إتمام موثقة برمز تحقق فريد" : "Verifiable completion certificate"}>
        <BadgeCheck className="h-3.5 w-3.5 fill-[#10b981]/20 text-[#10b981]" />
        <span className="text-[11px] font-medium text-[#10b981]">
          {lang === "ar" ? "شهادة موثقة" : "Certified"}
        </span>
      </div>
    </div>
  );
}

export default function CourseCard({ course }: CourseCardProps) {
  const { lang } = useTranslation();
  const title = lang === "ar" && course.titleAr ? course.titleAr : course.titleEn;
  const shortDesc = lang === "ar" && course.shortDescAr ? course.shortDescAr : course.shortDescEn;
  const categoryLabel = lang === "ar" && course.categoryNameAr ? course.categoryNameAr : course.categoryName;
  const coursePrice = Number.parseFloat(course.price || "0");
  const originalPrice = course.originalPrice ? Number.parseFloat(course.originalPrice) : 0;
  const discount = originalPrice > coursePrice && originalPrice > 0
    ? Math.round(((originalPrice - coursePrice) / originalPrice) * 100)
    : 0;
  const lvl = levelColors[course.level] || levelColors.beginner;
  const dir = lang === "ar" ? "rtl" : "ltr";
  const [thumbSrc, setThumbSrc] = useState(course.thumbnail);
  const [thumbError, setThumbError] = useState(false);
  // Adjust state during render when the prop changes (React docs pattern) —
  // the playlist sync updates the cover server-side while the card is mounted.
  const [prevThumbnail, setPrevThumbnail] = useState(course.thumbnail);
  if (prevThumbnail !== course.thumbnail) {
    setPrevThumbnail(course.thumbnail);
    setThumbSrc(course.thumbnail);
    setThumbError(false);
  }

  const onThumbError = () => {
    const next = youTubeThumbnailFallback(thumbSrc);
    if (next) {
      setThumbSrc(next);
      return;
    }
    setThumbError(true);
  };

  return (
    <TiltCard className="h-full">
      <Link to={`/courses/${course.slug}`} className="group block h-full outline-none focus-visible:ring-2 focus-visible:ring-[#06b6d4] rounded-2xl">
        <article
          className="brand-signature-card relative h-full overflow-hidden rounded-2xl border border-[#1e2d3d] bg-[#0d1420] transition-all duration-300
            group-hover:border-[rgba(6,182,212,0.4)] group-hover:shadow-[0_20px_60px_rgba(6,182,212,0.12)]
            group-hover:will-change-transform"
        >
          {/* ── Thumbnail ── */}
          <div className="relative h-48 overflow-hidden bg-gradient-to-br from-[#0a1628] to-[#1e2d3d]">
            {thumbError ? (
              <ThumbnailFallback title={title} />
            ) : (
              <img
                src={thumbSrc || "/hero-main.svg"}
                alt={title}
                loading="lazy"
                width="384"
                height="192"
                decoding="async"
                onError={onThumbError}
                className="h-full w-full object-cover transition-transform duration-700 group-hover:scale-[1.04]"
              />
            )}

            {/* Gradient overlay */}
            <div className="absolute inset-0 bg-gradient-to-t from-[#0d1420] via-transparent to-transparent" />

            {/* Top badges row */}
            <CourseCardBadges course={course} lang={lang} discount={discount} />

            {/* Duration — bottom right */}
            <div className="absolute bottom-3 end-3 flex items-center gap-1 rounded-lg bg-[rgba(10,14,23,0.75)] px-2 py-1 backdrop-blur-md">
              <Clock className="h-3 w-3 text-[#94a3b8]" />
              <span className="text-[11px] font-medium text-[#94a3b8]">
                {course.durationHours}{lang === "ar" ? "س" : "h"}
              </span>
            </div>
          </div>

          {/* ── Body ── */}
          <div className="p-5 flex flex-col h-[calc(100%-12rem)]">
            <div className="flex-1">
              {/* Category + Level */}
              <div className="flex items-center justify-between">
                <span className="text-[11px] font-semibold uppercase tracking-widest text-[#06b6d4]">
                  {categoryLabel || "Engineering"}
                </span>
                <span
                  className="rounded-md px-2 py-0.5 text-[10px] font-semibold uppercase tracking-wider"
                  style={{ background: lvl.bg, color: lvl.text }}
                >
                  {lang === "ar" ? lvl.labelAr : lvl.label}
                </span>
              </div>

              {/* Title */}
              <h3
                className="mt-2 line-clamp-2 text-[15px] font-bold leading-snug text-[#e8f0fe] transition-colors duration-200 group-hover:text-[#06b6d4]"
                dir={dir}
              >
                {title}
              </h3>

              {/* Short description */}
              {shortDesc && (
                <p
                  className="mt-1.5 line-clamp-2 text-[12px] leading-relaxed text-[#64748b]"
                  dir={dir}
                >
                  {shortDesc}
                </p>
              )}

              {/* ── Stats row ── */}
              <CourseCardStats lang={lang} rating={course.rating} reviewCount={course.reviewCount} studentCount={course.studentCount} />
            </div>

            {/* ── Separator ── */}
            <div className="my-4 h-px bg-gradient-to-r from-transparent via-[#1e2d3d] to-transparent" />

            {/* ── Price + CTA ── */}
            <div className="flex items-center justify-between mt-auto">
              <div>
                <CourseCardPrice coursePrice={coursePrice} originalPrice={originalPrice} lang={lang} />
              </div>

              {/* Animated CTA arrow */}
              <div className="flex h-8 w-8 items-center justify-center rounded-full border border-[#1e2d3d] bg-[#111827] transition-all duration-200 group-hover:border-[#06b6d4] group-hover:bg-[rgba(6,182,212,0.1)]">
                <ChevronRight className="h-4 w-4 text-[#475569] transition-colors group-hover:text-[#06b6d4] rtl:rotate-180" />
              </div>
            </div>
          </div>

          {/* Shimmer line on hover */}
          <div className="absolute bottom-0 start-0 h-[2px] w-0 bg-gradient-to-r from-[#06b6d4] to-[#0891b2] transition-all duration-500 group-hover:w-full" />

          {/* Premium shine sweep effect */}
          <div className="absolute inset-0 overflow-hidden rounded-2xl pointer-events-none">
            <div className="shimmer-shine" />
          </div>
        </article>
      </Link>
    </TiltCard>
  );
}