import Link from "next/link"

import { getAdminStats } from "@/lib/admin/queries"
import { getAllReports } from "@/lib/mdx"

function formatDate(date: string) {
  return new Date(date).toLocaleDateString("en-GB", {
    day: "numeric",
    month: "short",
    year: "numeric",
  })
}

export default async function AdminDashboardPage() {
  const stats = await getAdminStats()
  const recentReports = getAllReports().slice(0, 5)

  const statCards = [
    ["TOTAL REPORTS", stats.totalReports],
    ["PRO REPORTS", stats.proReports],
    ["FREE REPORTS", stats.freeReports],
    ["REGISTERED USERS", stats.totalUsers],
    ["SUBSCRIBERS", stats.totalSubscribers],
  ] as const

  return (
    <div className="max-w-[1000px]">
      <header>
        <h1 className="font-serif text-[2rem] font-normal text-[var(--color-text-primary)]">
          Dashboard
        </h1>
        <p className="mt-1 text-[14px] text-[var(--color-text-secondary)]">
          Welcome back. Here is what is happening on Parallax.
        </p>
      </header>

      <section className="mt-8 flex flex-wrap gap-4">
        {statCards.map(([label, value]) => (
          <div
            key={label}
            className="min-w-[160px] flex-1 rounded-lg border border-[var(--color-border)] bg-[var(--color-surface)] p-5"
          >
            <div className="mb-1 text-[10px] uppercase tracking-[0.16em] text-[var(--color-accent)]">
              {label}
            </div>
            <div className="font-serif text-[2rem] text-[var(--color-text-primary)]">
              {value}
            </div>
          </div>
        ))}
      </section>

      <section className="mt-10">
        <h2 className="mb-4 font-serif text-[1.2rem] font-normal text-[var(--color-text-primary)]">
          Quick actions
        </h2>
        <div className="flex flex-wrap gap-3">
          <Link
            href="/admin/reports/new"
            className="rounded bg-[var(--color-accent)] px-4 py-2 text-[13px] text-white transition hover:bg-[var(--color-accent-hover)]"
          >
            New Report
          </Link>
          <Link
            href="/admin/reports"
            className="rounded border border-[var(--color-border)] bg-[var(--color-surface)] px-4 py-2 text-[13px] text-[var(--color-text-primary)] transition hover:border-[var(--color-accent)]"
          >
            View Reports
          </Link>
          <Link
            href="/admin/users"
            className="rounded border border-[var(--color-border)] bg-[var(--color-surface)] px-4 py-2 text-[13px] text-[var(--color-text-primary)] transition hover:border-[var(--color-accent)]"
          >
            Manage Users
          </Link>
        </div>
      </section>

      <section className="mt-10">
        <h2 className="mb-4 font-serif text-[1.2rem] font-normal text-[var(--color-text-primary)]">
          Recent reports
        </h2>
        <div className="overflow-x-auto rounded-lg border border-[var(--color-border)]">
          <table className="w-full border-collapse text-left">
            <thead className="bg-[var(--color-surface)] text-[11px] uppercase tracking-[0.14em] text-[var(--color-accent)]">
              <tr>
                <th className="px-4 py-3 font-normal">Title</th>
                <th className="px-4 py-3 font-normal">Region</th>
                <th className="px-4 py-3 font-normal">Pro?</th>
                <th className="px-4 py-3 font-normal">Published</th>
                <th className="px-4 py-3 font-normal">Actions</th>
              </tr>
            </thead>
            <tbody>
              {recentReports.map((report) => (
                <tr
                  key={report.slug}
                  className="border-t border-[var(--color-border)] text-[13px]"
                >
                  <td className="px-4 py-3 text-[var(--color-text-primary)]">
                    {report.title}
                  </td>
                  <td className="px-4 py-3 text-[var(--color-text-secondary)]">
                    {report.region}
                  </td>
                  <td className="px-4 py-3">
                    {report.isPro ? (
                      <span className="rounded bg-[#7c3aed] px-2 py-1 text-[10px] font-semibold text-white">
                        PRO
                      </span>
                    ) : (
                      <span className="text-[var(--color-text-secondary)]">
                        Free
                      </span>
                    )}
                  </td>
                  <td className="px-4 py-3 text-[var(--color-text-secondary)]">
                    {formatDate(report.publishedAt)}
                  </td>
                  <td className="px-4 py-3">
                    <Link
                      href={`/admin/reports/${report.slug}`}
                      className="text-[13px] text-[var(--color-accent)] hover:underline"
                    >
                      Edit
                    </Link>
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      </section>
    </div>
  )
}
