---
import { profileConfig } from "../config";
import { url } from "../utils/url-utils";
import { execSync } from "child_process";

const currentYear = new Date().getFullYear();

let commitHash = "unknown";
let buildDate = "unknown";

try {
  commitHash = execSync("git rev-parse --short=7 HEAD").toString().trim();

  const date = new Date();
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, "0");
  const day = date.getDate().toString().padStart(2, "0");
  const hours = date.getHours().toString().padStart(2, "0");
  const minutes = date.getMinutes().toString().padStart(2, "0");
  const seconds = date.getSeconds().toString().padStart(2, "0");
  buildDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
} catch (e) {
  console.warn("Failed to get git info", e);
}
---

<!--<div class="border-t border-[var(--primary)] mx-16 border-dashed py-8 max-w-[var(--page-width)] flex flex-col items-center justify-center px-6">-->
<div
  class="transition border-t border-black/10 dark:border-white/15 my-10 border-dashed mx-4 md:mx-32"
>
</div>
<div class="card-base w-fit mx-auto rounded-xl mt-4 mb-4">
  <div class="transition text-50 text-sm text-center p-6">
    &copy; <span id="copyright-year">2024 - {currentYear}</span>
    <a
      class="transition link text-[var(--primary)] font-medium"
      target="_blank"
      href="https://space.bilibili.com/325903362"
    >
      {profileConfig.name}
    </a>，采用
    <a
      class="transition link text-[var(--primary)] font-medium"
      target="_blank"
      href="https://creativecommons.org/licenses/by-nc-sa/4.0/"
      >CC BY-NC-SA 4.0</a
    > 许可
    <br />
    <a
      class="transition link text-[var(--primary)] font-medium"
      target="_blank"
      href={url("rss.xml")}>RSS</a
    > /
    <a
      class="transition link text-[var(--primary)] font-medium"
      target="_blank"
      href={url("sitemap-index.xml")}>网站地图</a
    >
    <br />
    由
    <a
      class="transition link text-[var(--primary)] font-medium"
      target="_blank"
      href="https://astro.build">Astro</a
    > 和
    <a
      class="transition link text-[var(--primary)] font-medium"
      target="_blank"
      href="https://github.com/saicaca/fuwari">Fuwari</a
    > 强力驱动
    <br />
    本网站代码
    <a
      class="transition link text-[var(--primary)] font-medium"
      target="_blank"
      href="https://github.com/afoim/fuwari">已开源</a
    >
    <a
      class="transition link text-black/30 dark:text-white/30 hover:text-[var(--primary)] text-xs ml-1"
      target="_blank"
      href={`https://github.com/afoim/fuwari/commit/${commitHash}`}
      >({commitHash} @ {buildDate})</a
    >
    <br />

    <a
      class="transition link text-[var(--primary)] font-medium inline-flex items-center"
      href="https://beian.miit.gov.cn/#/Integrated/index"
      target="_blank"
      ><img
        alt=""
        src="/favicon/foot-icp.png"
        class="h-4 mr-1"
      />晋ICP备2025071728号-1</a
    >
    <br />
    <div class="server-info-wrapper flex items-center justify-center">
      <span class="server-info text-black/30 dark:text-white/30 text-xs"></span>
      <img
        class="server-icon h-5 ml-1 hidden bg-white rounded p-0.5"
        alt="Server Icon"
      />
    </div>

    <!-- <a class="transition link text-[var(--primary)] font-medium inline-flex items-center" href="https://beian.mps.gov.cn/#/query/webSearch?code=34010302002608" target="_blank"><img alt="" src="/favicon/foot-ga.png" class="h-4 mr-1">皖公网安备34010302002608号</a>
        <br> -->
  </div>
</div>

<script>
  function updateServerInfo(server) {
    const wrappers = document.querySelectorAll(".server-info-wrapper");
    wrappers.forEach((wrapper) => {
      const serverInfo = wrapper.querySelector(".server-info");
      const serverIcon = wrapper.querySelector(".server-icon");
      if (serverInfo) {
        if (server) {
          serverInfo.innerText = `访问节点：${server}`;
          if (serverIcon) {
            const serverLower = server.toLowerCase();
            if (serverLower === "edgeone-pages") {
              serverIcon.src = "/cdn/eo.png";
              serverIcon.classList.remove("hidden");
              serverInfo.classList.add("hidden");
            } else if (serverLower === "cloudflare") {
              serverIcon.src = "/cdn/cf.svg";
              serverIcon.classList.remove("hidden");
              serverInfo.classList.add("hidden");
            } else if (serverLower === "esa") {
              serverIcon.src = "/cdn/esa.svg";
              serverIcon.classList.remove("hidden");
              serverInfo.classList.add("hidden");
            } else {
              serverIcon.classList.add("hidden");
              serverInfo.classList.remove("hidden");
            }
          }
        } else {
          serverInfo.innerText = "访问节点：未知";
          serverInfo.classList.remove("hidden");
          if (serverIcon) serverIcon.classList.add("hidden");
        }
      }
    });
  }

  function initServerInfo() {
    const isDevMode = localStorage.getItem("dev-mode") === "true";
    if (isDevMode) {
      const devServer = localStorage.getItem("dev-server");
      updateServerInfo(devServer);
    } else {
      const url = window.location.href;
      fetch(url, { method: "HEAD" })
        .then((response) => {
          const server = response.headers.get("server");
          updateServerInfo(server);
        })
        .catch((error) => {
          console.error("Failed to fetch server info:", error);
          updateServerInfo(null);
        });
    }
  }

  initServerInfo();
  document.addEventListener("astro:after-swap", initServerInfo); // Support View Transitions if enabled
  // Support Swup if enabled (listen to content replacement)
  document.addEventListener("content:replace", initServerInfo);
</script>
