import type { Metadata } from "next";
import Link from "next/link";
import { moveTestimonialAction, toggleTestimonialAction } from "@/app/admin/actions";
import { MoveButtons } from "@/components/admin/MoveButtons";
import { DeleteTestimonialButton } from "@/components/admin/DeleteTestimonialButton";
import { getAllTestimonials } from "@/lib/cms";

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

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

  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">Testimonials</h1>
          <p className="mt-2 text-ink-soft">
            Client quotes shown in the homepage carousel. Published entries appear
            in the order listed here.
          </p>
        </div>
        <Link href="/admin/testimonials/new" className="btn btn-teal">
          New testimonial
        </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">
          Testimonial saved — the homepage carousel 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">
          Testimonial deleted.
        </p>
      ) : null}

      <div className="space-y-4">
        {testimonials.map((t, i) => (
          <article
            key={t.id}
            className="card-soft flex flex-wrap items-center gap-x-6 gap-y-4 p-5"
          >
            <MoveButtons
              action={moveTestimonialAction}
              id={t.id}
              position={i}
              count={testimonials.length}
            />
            <div className="min-w-0 flex-1">
              <div className="flex flex-wrap items-center gap-3">
                <Link
                  href={`/admin/testimonials/${t.id}/edit`}
                  className="font-display text-xl font-semibold text-ink transition-colors hover:text-teal"
                >
                  {t.author}
                </Link>
                <span
                  className={`rounded-full px-2.5 py-1 text-[0.68rem] font-bold uppercase tracking-wide ${
                    t.published
                      ? "bg-teal-soft text-teal-dark"
                      : "bg-amber-soft text-amber-dark"
                  }`}
                >
                  {t.published ? "Published" : "Hidden"}
                </span>
                <span className="text-xs font-semibold text-amber">
                  {"★".repeat(t.rating)}
                  <span className="text-linen">{"★".repeat(5 - t.rating)}</span>
                </span>
              </div>
              <p className="mt-1 line-clamp-2 text-sm text-ink-soft">“{t.quote}”</p>
              {t.role ? (
                <p className="mt-1 text-xs text-ink-mute">{t.role}</p>
              ) : null}
            </div>

            <div className="flex flex-wrap items-center gap-2">
              <form action={toggleTestimonialAction}>
                <input type="hidden" name="id" value={t.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"
                >
                  {t.published ? "Hide" : "Publish"}
                </button>
              </form>
              <Link
                href={`/admin/testimonials/${t.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>
              <DeleteTestimonialButton id={t.id} author={t.author} />
            </div>
          </article>
        ))}

        {testimonials.length === 0 ? (
          <div className="card-soft p-10 text-center">
            <p className="text-ink-soft">
              No testimonials yet. Add your first client quote to fill the
              homepage carousel.
            </p>
            <Link href="/admin/testimonials/new" className="btn btn-teal mt-6">
              Add your first testimonial
            </Link>
          </div>
        ) : null}
      </div>
    </div>
  );
}
