import type { Metadata } from "next";
import Link from "next/link";
import {
  deleteServiceAction,
  moveServiceAction,
  toggleServiceAction,
} from "@/app/admin/actions";
import { DeleteButton } from "@/components/admin/DeleteButton";
import { MoveButtons } from "@/components/admin/MoveButtons";
import { getServices } from "@/lib/cms";

export const metadata: Metadata = {
  title: "Services",
  robots: { index: false, follow: false },
};

export default async function AdminServicesPage({
  searchParams,
}: {
  searchParams: Promise<{ saved?: string; deleted?: string }>;
}) {
  const [{ saved, deleted }, services] = await Promise.all([
    searchParams,
    getServices(),
  ]);

  return (
    <div className="space-y-8">
      <header className="flex flex-wrap items-end justify-between gap-4">
        <div>
          <p className="text-sm font-semibold uppercase tracking-[0.14em] text-coral">
            Content
          </p>
          <h1 className="display-2 mt-2">Services</h1>
          <p className="mt-2 text-ink-soft">
            What you offer — shown on the services page and the homepage cards.
          </p>
        </div>
        <Link href="/admin/services/new" className="btn btn-teal">
          New service
        </Link>
      </header>

      {saved ? (
        <p className="rounded-2xl border border-teal/30 bg-teal-soft px-5 py-3 text-sm font-medium text-teal-dark">
          Saved “{saved}” — the website has been refreshed.
        </p>
      ) : null}
      {deleted ? (
        <p className="rounded-2xl border border-coral/30 bg-coral-soft px-5 py-3 text-sm font-medium text-coral-dark">
          Service deleted.
        </p>
      ) : null}

      <div className="space-y-4">
        {services.map((s, i) => (
          <article
            key={s.id}
            className="card-soft flex flex-wrap items-center gap-x-6 gap-y-4 p-5"
          >
            <MoveButtons
              action={moveServiceAction}
              id={s.id}
              position={i}
              count={services.length}
            />
            <div className="min-w-0 flex-1">
              <div className="flex flex-wrap items-center gap-3">
                <Link
                  href={`/admin/services/${s.id}/edit`}
                  className="font-display text-xl font-semibold text-ink transition-colors hover:text-teal"
                >
                  {s.title}
                </Link>
                <span
                  className={`rounded-full px-2.5 py-1 text-[0.68rem] font-bold uppercase tracking-wide ${
                    s.published
                      ? "bg-teal-soft text-teal-dark"
                      : "bg-amber-soft text-amber-dark"
                  }`}
                >
                  {s.published ? "Published" : "Hidden"}
                </span>
              </div>
              <p className="mt-1 line-clamp-1 text-sm text-ink-soft">{s.short}</p>
              <p className="mt-0.5 text-xs text-ink-mute">
                /services#{s.slug} · {s.includes.length} inclusions
              </p>
            </div>

            <div className="flex flex-wrap items-center gap-2">
              <form action={toggleServiceAction}>
                <input type="hidden" name="id" value={s.id} />
                <button
                  type="submit"
                  className="rounded-full border border-linen px-3.5 py-1.5 text-xs font-semibold text-ink-soft transition-colors hover:border-teal hover:text-teal"
                >
                  {s.published ? "Hide" : "Publish"}
                </button>
              </form>
              <Link
                href={`/admin/services/${s.id}/edit`}
                className="rounded-full bg-teal px-3.5 py-1.5 text-xs font-semibold text-white transition-colors hover:bg-teal-dark"
              >
                Edit
              </Link>
              <Link
                href={`/services#${s.slug}`}
                className="rounded-full px-3.5 py-1.5 text-xs font-semibold text-ink-soft transition-colors hover:bg-mint"
              >
                View
              </Link>
              <DeleteButton
                action={deleteServiceAction}
                id={s.id}
                label="Delete"
                confirm={`Delete “${s.title}”? This cannot be undone.`}
              />
            </div>
          </article>
        ))}

        {services.length === 0 ? (
          <div className="card-soft p-10 text-center">
            <p className="text-ink-soft">No services yet.</p>
            <Link href="/admin/services/new" className="btn btn-teal mt-6">
              Add your first service
            </Link>
          </div>
        ) : null}
      </div>
    </div>
  );
}
