import type { Metadata } from "next";
import { notFound } from "next/navigation";
import {
  SiteClosingCta,
  SiteEmptyState,
  SiteMetaRow,
  SitePageHero,
  SiteShell,
} from "@/components/site/site-shell";
import { resolveRequestTenant } from "@/core/tenant/request-tenant";
import { studioPageMetadata } from "@/core/tenant/studio-metadata";
import { getArchitectProfile } from "@/services/profile-service";
import { listPublishedPublications } from "@/services/publication-service";

export async function generateMetadata(): Promise<Metadata> {
  const resolved = await resolveRequestTenant();
  if (!resolved.ok) return { title: "Publications", robots: { index: false } };
  return studioPageMetadata(
    resolved.context.studioName,
    "Publications",
    `Magazines, interviews, and features covering ${resolved.context.studioName}.`,
  );
}

export default async function TenantPublicationsPage() {
  const resolved = await resolveRequestTenant();
  if (!resolved.ok) notFound();

  const [{ profile }, publications] = await Promise.all([
    getArchitectProfile(resolved.context),
    listPublishedPublications(resolved.context),
  ]);

  return (
    <SiteShell
      studioName={resolved.context.studioName}
      location={profile?.location}
      active="publications"
    >
      <main>
        <SitePageHero
          eyebrow="Press"
          title="Publications"
          description={`Magazines, interviews, and features covering ${resolved.context.studioName}.`}
        />

        <section className="studio-container pb-16 pt-10 sm:pb-20 sm:pt-12">
          {publications.length === 0 ? (
            <SiteEmptyState
              title="Press archive in progress"
              description="Published features and interviews will be listed here once the studio adds them."
            />
          ) : (
            <ul className="divide-y divide-[var(--archi-line)] border-y border-[var(--archi-line)]">
              {publications.map((pub) => {
                const inner = (
                  <>
                    <h2 className="font-[family-name:var(--font-display)] text-[clamp(1.5rem,4vw,2.25rem)] tracking-tight transition group-hover:opacity-70 sm:text-4xl">
                      {pub.title}
                    </h2>
                    <SiteMetaRow
                      className="mt-3"
                      items={[
                        pub.publisher,
                        pub.year != null ? String(pub.year) : null,
                        pub.kind,
                      ]}
                    />
                    {pub.description ? (
                      <p className="mt-5 max-w-2xl whitespace-pre-wrap text-base leading-8 text-[var(--archi-ink-muted)]">
                        {pub.description}
                      </p>
                    ) : null}
                  </>
                );

                return (
                  <li key={pub.id} className="py-10 lg:py-14">
                    {pub.url ? (
                      <a
                        href={pub.url}
                        target="_blank"
                        rel="noreferrer"
                        className="group block"
                      >
                        {inner}
                        <span className="mt-5 inline-flex text-sm tracking-wide underline decoration-[var(--archi-gold)] underline-offset-8">
                          Read →
                        </span>
                      </a>
                    ) : (
                      <div>{inner}</div>
                    )}
                  </li>
                );
              })}
            </ul>
          )}
        </section>

        <SiteClosingCta studioName={resolved.context.studioName} />
      </main>
    </SiteShell>
  );
}
