import { Star } from "lucide-react";
import { TrackedGitHubStar } from "@/components/analytics/tracked-github-star";

const GITHUB_REPO = "https://github.com/Work90210/APIFold";

async function getStarCount(): Promise<string> {
  try {
    const headers: Record<string, string> = {
      Accept: "application/vnd.github.v3+json",
    };
    if (process.env.GITHUB_TOKEN) {
      headers.Authorization = `Bearer ${process.env.GITHUB_TOKEN}`;
    }

    const res = await fetch(
      "https://api.github.com/repos/Work90210/APIFold",
      {
        next: { revalidate: 3600 },
        headers,
      },
    );

    if (!res.ok) return "0";

    const data = await res.json();
    const count = data.stargazers_count;
    return typeof count === "number"
      ? new Intl.NumberFormat().format(count)
      : "0";
  } catch {
    return "0";
  }
}

export async function GithubStars() {
  const stars = await getStarCount();

  return (
    <TrackedGitHubStar
      href={GITHUB_REPO}
      location="hero"
      className="inline-flex h-12 items-center gap-2 rounded-md border border-border bg-transparent px-6 text-base font-semibold text-foreground transition-all duration-300 hover:bg-muted/50 active:scale-[0.98]"
    >
      <Star className="h-4 w-4 fill-amber-400 text-amber-400" />
      Star on GitHub
      <span className="ml-1 rounded-md border border-border px-2 py-0.5 text-sm tabular-nums text-muted-foreground">
        {stars}
      </span>
    </TrackedGitHubStar>
  );
}
