import { Camera, Flame, Snowflake } from "lucide-react"
import { Button } from "@/components/ui/button"
import GoalRing from "@/components/practice/goal-ring.component"
import WeekStrip from "@/components/practice/week-strip.component"
import { StreakStateDto } from "@/lib/dto/practice.dto"
import { faNumber } from "@/lib/helpers/format.helper"
import { cn } from "@/lib/utils"

/** After this Tehran hour, an unpractised day shows how many hours are left. */
const LATE_HOUR = 17

export default function TodayCard({ streak, onLog }: { streak: StreakStateDto; onLog: () => void }) {
  const done = streak.practised_today

  const status = done
    ? `امروز ثبت شد · فردا روز ${faNumber((streak.current ?? 0) + 1)}`
    : streak.hour >= LATE_HOUR
      ? null
      : streak.current > 0
        ? "یک تمرین کوتاه، استمرارت را نگه می‌دارد"
        : "از امروز شروع کن؛ کمی، ولی هر روز"

  return (
    <section
      aria-label="تمرین امروز"
      className={cn(
        "rounded-card border p-4 bg-canvas",
        done ? "border-sage/40 shadow-card" : "border-sienna/30 shadow-raised"
      )}
    >
      <div className="flex items-center">
        <span
          className={cn(
            "w-14 h-14 rounded-2xl grid place-items-center shrink-0",
            done ? "bg-gradient-to-br from-sienna to-ochre text-canvas" : "bg-paper-2 text-ink-soft"
          )}
          aria-hidden="true"
        >
          <Flame className="w-7 h-7" />
        </span>

        <div className="flex-1 min-w-0 mr-3">
          <p className="text-display font-extrabold leading-none tabular-nums">
            {faNumber(streak.current)} <span className="text-body font-bold">روز پیاپی</span>
          </p>
          <p className="text-caption text-ink-soft mt-1.5">
            {status ?? (
              <>
                <b className="text-sienna">{faNumber(streak.hours_left)} ساعت</b> تا پایان امروز فرصت داری
              </>
            )}
          </p>
        </div>

        <div className="flex flex-col items-center">
          <GoalRing value={streak.xp?.today ?? 0} goal={streak.xp?.goal ?? 0} />
          <span className="text-micro text-ink-soft mt-0.5">از {faNumber(streak.xp?.goal ?? 0)} XP</span>
        </div>
      </div>

      <div className="mt-4">
        <WeekStrip days={streak.week ?? []} />
      </div>

      {!done && (
        <Button size="lg" className="w-full h-12 rounded-2xl mt-4" onClick={onLog}>
          <Camera className="w-5 h-5" />
          ثبت تمرین امروز
          <span className="text-caption opacity-90" dir="ltr">+20 XP</span>
        </Button>
      )}

      <div className="flex items-center justify-between text-caption text-ink-soft mt-3">
        <span>رکورد: {faNumber(streak.best)} روز</span>
        <span className="flex items-center gap-1 text-ultra">
          <Snowflake className="w-3.5 h-3.5" aria-hidden="true" />
          {faNumber(streak.freezes)} یخ‌زدگی
        </span>
      </div>

      {streak.freeze_used_today && (
        <p className="text-caption text-ultra mt-2 flex items-center gap-1">
          <Snowflake className="w-3.5 h-3.5" aria-hidden="true" />
          دیروز جا ماندی؛ یک یخ‌زدگی استمرارت را نجات داد.
        </p>
      )}
    </section>
  )
}
