"use client";

import { BarChart3, ChevronDown, Flame, Play, Sparkles, TrendingUp, Users } from "lucide-react";
import type { LucideIcon } from "lucide-react";
import { ScoreRing } from "@/components/score-ring";
import { Logo } from "@/components/logo";
import { trendingProducts, dashboardSummary } from "@/lib/mock-data";
import { useLanguage } from "@/lib/i18n/context";

const sparkline = "M4 92 C32 88 42 76 62 80 S92 70 112 74 S142 56 164 61 S194 45 214 49 S242 31 268 36 S298 20 326 24";

export function DashboardPreview() {
  const { t } = useLanguage();
  const featured = trendingProducts[0];
  const featuredLabel = t.products.find((p) => p.id === featured.id);

  return (
    <div className="relative overflow-hidden rounded-[28px] border border-border bg-card shadow-[0_32px_100px_rgba(15,23,42,.12)]">
      <div className="pointer-events-none absolute -right-24 -top-28 size-80 rounded-full bg-brand-cyan/10 blur-3xl" />
      <div className="pointer-events-none absolute -bottom-32 left-1/3 size-96 rounded-full bg-brand-purple/10 blur-3xl" />

      <div className="relative flex flex-wrap items-center justify-between gap-4 border-b border-border px-5 py-4 sm:px-7">
        <div className="flex items-center gap-5">
          <Logo size="large" />
          <span className="hidden h-5 w-px bg-border sm:block" />
          <div className="hidden sm:block">
            <p className="text-[10px] font-semibold uppercase tracking-[0.18em] text-brand-cyan">{t.dashboardPreview.titleBar}</p>
            <p className="mt-0.5 text-xs text-muted-foreground">{t.dashboardPreview.livePreview}</p>
          </div>
        </div>
        <div className="flex items-center gap-2 text-xs">
          <span className="inline-flex items-center gap-1.5 rounded-full border border-border bg-secondary px-3 py-1.5 text-muted-foreground">
            <span className="size-1.5 rounded-full bg-emerald-400 shadow-[0_0_10px_rgba(52,211,153,.8)]" />
            Signals updating
          </span>
          <span className="hidden items-center gap-1 rounded-full border border-border bg-secondary px-3 py-1.5 text-muted-foreground sm:inline-flex">
            Last 7 days <ChevronDown className="size-3" />
          </span>
        </div>
      </div>

      <div className="relative grid gap-5 p-5 sm:p-7 lg:grid-cols-[1.05fr_.95fr]">
        <div className="space-y-5">
          <div className="relative overflow-hidden rounded-2xl border border-border bg-secondary/60 p-5 shadow-sm">
            <div className="absolute right-0 top-0 h-full w-1/2 bg-gradient-to-l from-brand-cyan/10 to-transparent" />
            <div className="relative flex items-start justify-between gap-4">
              <div className="flex min-w-0 items-center gap-3">
                <div className="flex size-12 shrink-0 items-center justify-center rounded-xl bg-gradient-to-br from-amber-300/25 to-brand-cyan/20 text-2xl ring-1 ring-border">
                  {featured.emoji}
                </div>
                <div className="min-w-0">
                  <p className="truncate text-sm font-semibold text-foreground">{featuredLabel?.name ?? featured.name}</p>
                  <p className="mt-1 text-xs text-muted-foreground">{featuredLabel?.category ?? featured.category} <span className="mx-1 text-muted-foreground/60">·</span> {featured.price}</p>
                </div>
              </div>
              <ScoreRing value={featured.opportunityScore} size={76} strokeWidth={6} label={t.dashboardPreview.score} className="shrink-0" />
            </div>
            <div className="relative mt-5 flex flex-wrap items-center gap-2 text-xs">
              <span className="rounded-full border border-emerald-400/20 bg-emerald-400/10 px-2.5 py-1 font-medium text-emerald-300">Low competition</span>
              <span className="inline-flex items-center gap-1 rounded-full border border-brand-cyan/20 bg-brand-cyan/10 px-2.5 py-1 font-medium text-brand-cyan"><TrendingUp className="size-3" /> +{featured.weeklyGrowth}% this week</span>
            </div>
            <div className="relative mt-5 h-28 overflow-hidden rounded-xl bg-background/80 px-1 pt-2">
              <svg viewBox="0 0 330 112" className="h-full w-full" role="img" aria-label={t.dashboardPreview.trendChartAlt} preserveAspectRatio="none">
                <defs><linearGradient id="landingSignalFill" x1="0" x2="0" y1="0" y2="1"><stop offset="0" stopColor="var(--brand-cyan)" stopOpacity=".25" /><stop offset="1" stopColor="var(--brand-cyan)" stopOpacity="0" /></linearGradient></defs>
                <path d={`${sparkline} L326 112 L4 112 Z`} fill="url(#landingSignalFill)" />
                <path d={sparkline} fill="none" stroke="var(--brand-cyan)" strokeLinecap="round" strokeWidth="3" />
              </svg>
              <div className="absolute inset-x-3 bottom-2 flex justify-between text-[10px] text-muted-foreground"><span>7 days ago</span><span>today</span></div>
            </div>
          </div>

          <div className="grid grid-cols-3 gap-2.5">
            <Signal label={t.dashboardPreview.productSaturation} value={`${dashboardSummary.productSaturation}%`} trend="healthy" icon={BarChart3} />
            <Signal label={t.dashboardPreview.activeCreators} value={`${dashboardSummary.activeCreators}`} trend="active" icon={Users} />
            <Signal label={t.dashboardPreview.viralVideos} value={`${dashboardSummary.viralVideos}`} trend="rising" icon={Play} />
          </div>
        </div>

        <div className="flex flex-col rounded-2xl border border-border bg-card/80 p-5 shadow-sm">
          <div className="flex items-center justify-between">
            <div><p className="flex items-center gap-2 text-sm font-semibold text-foreground"><Flame className="size-4 text-brand-magenta" /> {t.dashboardPreview.trendingProducts}</p><p className="mt-1 text-xs text-muted-foreground">Ranked by momentum, demand and creator activity</p></div>
            <Sparkles className="size-4 text-brand-purple" />
          </div>
          <div className="mt-5 divide-y divide-border">
            {trendingProducts.slice(0, 4).map((product, index) => {
              const label = t.products.find((p) => p.id === product.id);
              return <div key={product.id} className="flex items-center gap-3 py-3 first:pt-0 last:pb-0">
                <span className="flex size-6 shrink-0 items-center justify-center rounded-full bg-secondary text-[10px] font-semibold text-muted-foreground">0{index + 1}</span>
                <span className="flex size-9 shrink-0 items-center justify-center rounded-lg bg-secondary text-base">{product.emoji}</span>
                <div className="min-w-0 flex-1"><p className="truncate text-xs font-medium text-foreground">{label?.name ?? product.name}</p><p className="mt-1 text-[10px] text-muted-foreground">{label?.category ?? product.category}</p></div>
                <div className="text-right"><p className="text-sm font-semibold text-brand-blue">{product.opportunityScore}</p><p className="text-[10px] text-muted-foreground">score</p></div>
              </div>;
            })}
          </div>
          <div className="mt-auto flex items-center justify-between border-t border-border pt-4 text-xs"><span className="text-muted-foreground">{t.dashboardPreview.estimatedSales}</span><span className="font-semibold text-foreground">{dashboardSummary.estimatedSales}</span></div>
        </div>
      </div>
    </div>
  );
}

function Signal({ label, value, trend, icon: Icon }: { label: string; value: string; trend: string; icon: LucideIcon }) {
  return <div className="rounded-xl border border-border bg-secondary/60 p-3 shadow-sm"><div className="flex items-center justify-between"><Icon className="size-3.5 text-muted-foreground" /><span className="text-[9px] uppercase tracking-wide text-success">{trend}</span></div><p className="mt-2 text-lg font-semibold text-foreground">{value}</p><p className="truncate text-[10px] text-muted-foreground">{label}</p></div>;
}
