"use client";

import { useEffect, useState } from "react";
import { Clock as ClockIcon } from "lucide-react";
import type { Profile } from "@/lib/constants";
import { card } from "./style";

export default function Clock({ profile }: { profile: Profile }) {
  const accent = profile.accent || "#f5caca";
  const [now, setNow] = useState<Date | null>(null);
  const [tz, setTz] = useState("Local time");

  useEffect(() => {
    setTz(Intl.DateTimeFormat().resolvedOptions().timeZone || "Local time");
    setNow(new Date());
    const t = setInterval(() => setNow(new Date()), 1000);
    return () => clearInterval(t);
  }, []);

  const time = now
    ? new Intl.DateTimeFormat("en-US", { hour: "2-digit", minute: "2-digit", second: "2-digit", hour12: false }).format(now)
    : "--:--:--";
  const date = now
    ? new Intl.DateTimeFormat("en-US", { weekday: "short", month: "short", day: "numeric" }).format(now)
    : "Local date";

  return (
    <div style={{ ...card(accent), display: "flex", alignItems: "center", gap: 14 }}>
      <ClockIcon size={30} style={{ color: accent }} />
      <div style={{ textAlign: "left" }}>
        <div style={{ fontSize: 22, fontWeight: 900, fontVariantNumeric: "tabular-nums" }}>{time}</div>
        <div style={{ fontSize: 12, color: "#240808" }}>
          {date}{!profile.hide_timezone && <> · {tz.replace(/_/g, " ")}</>}
        </div>
      </div>
    </div>
  );
}
