import Link from "next/link"

import DeleteReportButton from "@/components/admin/DeleteReportButton"
import { toggleReportPro } from "@/lib/admin/actions"
import { getAllReports } from "@/lib/mdx"

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

async function toggleProAction(formData: FormData) {
  "use server"

  const slug = String(formData.get("slug") ?? "")
  const isPro = formData.get("isPro") === "true"

  await toggleReportPro(slug, isPro)
}

export default function AdminReportsPage() {
  const reports = getAllReports()

  return (
    <div>
      <header className="mb-8 flex flex-wrap items-center justify-between gap-4">
        <div>
          <div className="flex items-center gap-3">
            <h1 className="font-serif text-[2rem] font-normal text-[var(--color-text-primary)]">
              All Reports
            </h1>
            <span className="rounded border border-[var(--color-border)] bg-[var(--color-surface)] px-2 py-1 text-[12px] text-[var(--color-text-secondary)]">
              {reports.length}
            </span>
          </div>
          <p className="mt-1 text-[14px] text-[var(--color-text-secondary)]">
            Manage every MDX report in the filesystem archive.
          </p>
        </div>
        <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>
      </header>

      <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">Topic</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>
            {reports.map((report) => (
              <tr
                key={report.slug}
                className="border-t border-[var(--color-border)] text-[13px]"
              >
                <td className="max-w-[300px] truncate px-4 py-3 text-[14px] text-[var(--color-text-primary)]">
                  {report.title}
                </td>
                <td className="px-4 py-3 text-[12px] text-[var(--color-text-secondary)]">
                  {report.region}
                </td>
                <td className="px-4 py-3 text-[12px] text-[var(--color-text-secondary)]">
                  {report.topic}
                </td>
                <td className="px-4 py-3">
                  <form action={toggleProAction}>
                    <input type="hidden" name="slug" value={report.slug} />
                    <input
                      type="hidden"
                      name="isPro"
                      value={String(!report.isPro)}
                    />
                    <button
                      type="submit"
                      className={`cursor-pointer rounded px-2 py-1 text-[10px] font-semibold ${
                        report.isPro
                          ? "bg-[#7c3aed] text-white"
                          : "bg-[var(--color-border)] text-[var(--color-text-secondary)]"
                      }`}
                    >
                      {report.isPro ? "PRO" : "Free"}
                    </button>
                  </form>
                </td>
                <td className="px-4 py-3 text-[var(--color-text-secondary)]">
                  {formatDate(report.publishedAt)}
                </td>
                <td className="px-4 py-3">
                  <div className="flex items-center gap-3">
                    <Link
                      href={`/admin/reports/${report.slug}`}
                      className="text-[13px] text-[var(--color-accent)] hover:underline"
                    >
                      Edit
                    </Link>
                    <DeleteReportButton slug={report.slug} />
                  </div>
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    </div>
  )
}
