{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "credit-usage-card-base",
  "type": "registry:component",
  "title": "Credit Usage Card (base)",
  "description": "Theme-ready base variant of Displays credit consumption progress with clear limits and usage breakdown..",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "files": [
    {
      "path": "components/watermelon/credit-usage-card.tsx",
      "type": "registry:component",
      "content": "import React, { useState, useRef, useEffect } from 'react';\nimport { motion, AnimatePresence } from 'motion/react';\nimport {\n  MoreVertical,\n  Download,\n  ChevronDown,\n  Check,\n  Printer,\n  Share2,\n  RefreshCw,\n} from 'lucide-react';\n\ninterface UsageHistoryItem {\n  date: string;\n  model: string;\n  credits: string;\n  cost: string;\n}\n\ninterface CreditUsageCardProps {\n  usedCreditsPercent?: number;\n  totalCreditsLabel?: string;\n  creditsUsedLabel?: string;\n  creditsLeftLabel?: string;\n  usageHistory?: UsageHistoryItem[];\n  onAutoSwitchChange?: (enabled: boolean) => void;\n  onManagePlan?: () => void;\n  onViewAll?: () => void;\n}\n\nconst PERIOD_OPTIONS = ['7 Days', '14 Days', '30 Days', '90 Days', '12 Months'];\n\nconst popoverAnim = {\n  initial: { opacity: 0, y: 4, scale: 0.98 },\n  animate: { opacity: 1, y: 0, scale: 1 },\n  exit: { opacity: 0, y: 4, scale: 0.98 },\n  transition: { type: 'spring' as const, stiffness: 450, damping: 25 },\n} as const;\n\nexport const CreditUsageCard: React.FC<CreditUsageCardProps> = ({\n  usedCreditsPercent = 56.4,\n  totalCreditsLabel = '100M CREDITS',\n  creditsUsedLabel = '56.4M',\n  creditsLeftLabel = '43.6M',\n  usageHistory = [],\n  onAutoSwitchChange,\n  onManagePlan,\n  onViewAll,\n}) => {\n  const [autoSwitch, setAutoSwitch] = useState(true);\n  const [activePopover, setActivePopover] = useState<'more' | 'period' | null>(\n    null,\n  );\n  const [selectedPeriod, setSelectedPeriod] = useState('30 Days');\n  const [downloadDone, setDownloadDone] = useState(false);\n  const segments = 75;\n\n  const moreRef = useRef<HTMLDivElement>(null);\n  const periodRef = useRef<HTMLDivElement>(null);\n\n  const handleToggleAutoSwitch = () => {\n    const newState = !autoSwitch;\n    setAutoSwitch(newState);\n    onAutoSwitchChange?.(newState);\n  };\n\n  const handleDownload = () => {\n    // Simulate CSV export\n    const headers = ['Date', 'Model', 'Credits', 'Cost'];\n    const rows = usageHistory.map((r) => [r.date, r.model, r.credits, r.cost]);\n    const csv = [headers, ...rows].map((r) => r.join(',')).join('\\n');\n    const blob = new Blob([csv], { type: 'text/csv' });\n    const url = URL.createObjectURL(blob);\n    const a = document.createElement('a');\n    a.href = url;\n    a.download = 'usage-history.csv';\n    a.click();\n    URL.revokeObjectURL(url);\n    setDownloadDone(true);\n    setTimeout(() => setDownloadDone(false), 2000);\n  };\n\n  useEffect(() => {\n    const handler = (e: MouseEvent) => {\n      if (moreRef.current && !moreRef.current.contains(e.target as Node)) {\n        setActivePopover((prev) => (prev === 'more' ? null : prev));\n      }\n      if (periodRef.current && !periodRef.current.contains(e.target as Node)) {\n        setActivePopover((prev) => (prev === 'period' ? null : prev));\n      }\n    };\n    document.addEventListener('mousedown', handler);\n    return () => document.removeEventListener('mousedown', handler);\n  }, []);\n\n  return (\n    <div className=\"theme-injected flex w-full flex-col items-center justify-center bg-transparent p-4 font-sans transition-colors duration-500 sm:p-8 md:p-12\">\n      <div className=\"bg-card border-border text-foreground w-full max-w-135 overflow-hidden rounded-xl border font-sans shadow-lg transition-all duration-500 select-none\">\n        {/* Top Header */}\n        <div className=\"bg-muted/50 flex flex-col items-start justify-between gap-4 px-4 py-5 sm:flex-row sm:items-center sm:px-6 md:px-8\">\n          <div>\n            <h3 className=\"text-muted-foreground mb-1 text-[10px] font-bold tracking-[0.2em] uppercase\">\n              Credits Used\n            </h3>\n            <span className=\"text-foreground font-sans text-2xl font-medium sm:text-3xl\">\n              {usedCreditsPercent}%\n            </span>\n          </div>\n\n          <div className=\"mt-1 flex items-center gap-2 self-end sm:self-auto\">\n            <span className=\"text-muted-foreground max-w-40 text-right text-[10px] leading-tight font-bold tracking-wider uppercase\">\n              Auto-switch to cheaper model at limit\n            </span>\n            <button\n              title=\"toggle\"\n              onClick={handleToggleAutoSwitch}\n              className={`relative flex h-5 w-10 shrink-0 items-center rounded-full border p-0.5 transition-colors duration-200 ${\n                autoSwitch\n                  ? 'bg-primary/15 border-primary/40'\n                  : 'bg-muted border-border'\n              }`}\n            >\n              <motion.div\n                animate={{ x: autoSwitch ? 18.5 : 0 }}\n                transition={{ type: 'spring', stiffness: 500, damping: 30 }}\n                className={`h-3.5 w-4 ${autoSwitch ? 'bg-primary' : 'bg-muted-foreground'} rounded-full shadow-xs`}\n              />\n            </button>\n          </div>\n        </div>\n\n        {/* Progress Bar */}\n        <div className=\"bg-muted/50 flex h-3 gap-px px-4 sm:gap-1 sm:px-6 md:px-8\">\n          {[...Array(segments)].map((_, i) => {\n            const isFilled = i < (usedCreditsPercent / 100) * segments;\n            return (\n              <div\n                key={i}\n                className={`flex-1 rounded-sm transition-all duration-700 ${\n                  isFilled ? 'bg-primary' : 'bg-muted'\n                }`}\n                style={{ opacity: isFilled ? 1 - i * 0.004 : 1 }}\n              />\n            );\n          })}\n        </div>\n\n        <div className=\"bg-muted/50 flex items-center justify-between px-4 py-4 text-xs font-bold sm:px-6 md:px-8\">\n          <span className=\"text-muted-foreground\">\n            {creditsUsedLabel}{' '}\n            <span className=\"text-muted-foreground/70\">\n              / {totalCreditsLabel}\n            </span>\n          </span>\n          <span className=\"text-muted-foreground\">\n            {creditsLeftLabel}{' '}\n            <span className=\"text-muted-foreground/70 xs:inline hidden\">\n              CREDITS LEFT\n            </span>\n          </span>\n        </div>\n\n        <div className=\"border-border h-px w-full border-b border-dashed\" />\n\n        {/* History Header */}\n        <div className=\"bg-muted/50 flex flex-row flex-wrap items-center justify-between gap-2 px-4 pt-4 pb-4 sm:flex-nowrap sm:px-6 md:px-8\">\n          <div className=\"flex flex-shrink-0 items-center gap-2\">\n            <h4 className=\"text-foreground font-sans text-sm font-medium whitespace-nowrap sm:text-base\">\n              Usage History\n            </h4>\n            <button\n              onClick={onViewAll}\n              title=\"view all\"\n              className=\"border-border text-muted-foreground hover:bg-accent hover:text-accent-foreground shrink-0 rounded-md border px-2 py-1 text-center text-[9px] whitespace-nowrap transition-colors active:scale-95\"\n            >\n              View all\n            </button>\n          </div>\n\n          {/* Period selector */}\n          <div ref={periodRef} className=\"relative flex-shrink-0\">\n            <button\n              title=\"period\"\n              onClick={() =>\n                setActivePopover((prev) =>\n                  prev === 'period' ? null : 'period',\n                )\n              }\n              className=\"border-border text-muted-foreground hover:bg-accent hover:text-accent-foreground flex items-center gap-1 rounded-md border px-2 py-1 text-[9px] whitespace-nowrap transition-all sm:gap-2\"\n            >\n              {selectedPeriod}{' '}\n              <ChevronDown\n                size={10}\n                className={`transition-transform duration-200 ${activePopover === 'period' ? 'rotate-180' : ''}`}\n              />\n            </button>\n            <AnimatePresence>\n              {activePopover === 'period' && (\n                <motion.div\n                  {...popoverAnim}\n                  className=\"bg-popover text-popover-foreground border-border absolute top-full right-0 z-50 mt-2 w-36 overflow-hidden rounded-lg border py-1 shadow-xl\"\n                >\n                  {PERIOD_OPTIONS.map((opt) => (\n                    <button\n                      key={opt}\n                      onClick={() => {\n                        setSelectedPeriod(opt);\n                        setActivePopover(null);\n                      }}\n                      className={`flex w-full items-center justify-between px-3 py-2 text-left text-xs transition-colors ${selectedPeriod === opt ? 'bg-primary/10 text-primary' : 'hover:bg-accent'}`}\n                    >\n                      {opt}\n                      {selectedPeriod === opt && <Check size={12} />}\n                    </button>\n                  ))}\n                </motion.div>\n              )}\n            </AnimatePresence>\n          </div>\n        </div>\n\n        {/* Table Wrapper */}\n        <div className=\"no-scrollbar bg-muted/50 overflow-x-auto\">\n          <div className=\"border-border min-w-[500px] space-y-0.5 border-b px-4 py-2 sm:px-6 md:px-8\">\n            <div className=\"text-muted-foreground grid grid-cols-4 px-1 pb-2 text-[10px] font-bold tracking-wider uppercase\">\n              <span>Date</span>\n              <span>Model</span>\n              <span className=\"text-right\">Credits</span>\n              <span className=\"text-right\">Cost</span>\n            </div>\n            {usageHistory.length > 0 ? (\n              usageHistory.map((row, idx) => (\n                <div\n                  key={idx}\n                  className=\"border-border/50 text-muted-foreground hover:bg-accent/30 group grid grid-cols-4 border-t px-1 py-2.5 text-[10.5px] transition-colors\"\n                >\n                  <span className=\"group-hover:text-foreground\">\n                    {row.date}\n                  </span>\n                  <span className=\"group-hover:text-foreground truncate pr-2 font-medium\">\n                    {row.model}\n                  </span>\n                  <span className=\"group-hover:text-foreground text-right\">\n                    {row.credits}\n                  </span>\n                  <span className=\"group-hover:text-foreground text-right font-bold\">\n                    {row.cost}\n                  </span>\n                </div>\n              ))\n            ) : (\n              <div className=\"text-muted-foreground border-border/50 border-t py-8 text-center text-xs\">\n                No usage history available\n              </div>\n            )}\n          </div>\n        </div>\n\n        {/* Footer*/}\n        <div className=\"bg-card flex flex-col items-center justify-between gap-4 px-4 pt-4 pb-4 sm:flex-row sm:px-6 md:px-8\">\n          <div className=\"text-muted-foreground flex items-center gap-3 self-start sm:self-auto\">\n            {/* More Options */}\n            <div ref={moreRef} className=\"relative flex items-center\">\n              <button\n                title=\"more\"\n                onClick={() =>\n                  setActivePopover((prev) => (prev === 'more' ? null : 'more'))\n                }\n                className=\"flex items-center justify-center\"\n              >\n                <MoreVertical\n                  size={16}\n                  className=\"hover:text-foreground cursor-pointer transition-colors\"\n                />\n              </button>\n              <AnimatePresence>\n                {activePopover === 'more' && (\n                  <motion.div\n                    {...popoverAnim}\n                    className=\"bg-popover text-popover-foreground border-border absolute bottom-full left-0 z-50 mb-2 w-44 overflow-hidden rounded-lg border py-1 shadow-xl\"\n                  >\n                    {[\n                      {\n                        label: 'Export CSV',\n                        icon: <Download size={14} />,\n                        action: handleDownload,\n                      },\n                      {\n                        label: 'Print history',\n                        icon: <Printer size={14} />,\n                        action: () => {\n                          window.print();\n                          setActivePopover(null);\n                        },\n                      },\n                      {\n                        label: 'Share report',\n                        icon: <Share2 size={14} />,\n                        action: () => setActivePopover(null),\n                      },\n                      {\n                        label: 'Refresh data',\n                        icon: <RefreshCw size={14} />,\n                        action: () => setActivePopover(null),\n                      },\n                    ].map((opt) => (\n                      <button\n                        key={opt.label}\n                        onClick={opt.action}\n                        className=\"hover:bg-accent hover:text-accent-foreground flex w-full items-center gap-3 px-3 py-2.5 text-left text-xs transition-colors\"\n                      >\n                        <span className=\"text-muted-foreground/70\">\n                          {opt.icon}\n                        </span>\n                        {opt.label}\n                      </button>\n                    ))}\n                  </motion.div>\n                )}\n              </AnimatePresence>\n            </div>\n\n            <div className=\"bg-border h-4 w-px\" />\n\n            {/* Download Button */}\n            <button\n              title=\"download\"\n              onClick={handleDownload}\n              className=\"group relative flex items-center justify-center\"\n            >\n              <motion.div\n                animate={\n                  downloadDone\n                    ? { scale: [1, 1.25, 1], rotate: [0, 5, -5, 0] }\n                    : {}\n                }\n                className={`flex items-center justify-center transition-colors ${downloadDone ? 'text-green-500' : 'hover:text-foreground'}`}\n              >\n                <Download size={16} />\n              </motion.div>\n            </button>\n          </div>\n\n          <div className=\"flex w-full items-center justify-between gap-2 sm:w-auto sm:justify-end\">\n            <div className=\"flex items-center gap-1.5 py-1\">\n              <div className=\"bg-primary text-primary-foreground flex h-4 w-4 items-center justify-center rounded-sm text-[10px] font-semibold\">\n                S\n              </div>\n              <span className=\"text-muted-foreground text-[9px] font-bold tracking-tighter uppercase\">\n                Billing via Stripe\n              </span>\n            </div>\n            <button\n              title=\"plans\"\n              onClick={onManagePlan}\n              className=\"border-border text-foreground hover:bg-foreground hover:text-background rounded-md border px-3 py-2 font-sans text-xs font-normal whitespace-nowrap transition-all active:scale-95\"\n            >\n              Manage plan\n            </button>\n          </div>\n        </div>\n      </div>\n\n      <style>{`\n        .no-scrollbar::-webkit-scrollbar { display: none; }\n        .no-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }\n      `}</style>\n    </div>\n  );\n};\n"
    }
  ]
}
