import type { Metadata } from "next";
import { notFound } from "next/navigation";
import {
  ProseBlock,
  SiteClosingCta,
  SiteEmptyState,
  SitePageHero,
  SiteSection,
  SiteShell,
  SiteLink,
} 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 { listPublishedTeam } from "@/services/team-service";

export async function generateMetadata(): Promise<Metadata> {
  const resolved = await resolveRequestTenant();
  if (!resolved.ok) return { title: "Studio", robots: { index: false } };
  return studioPageMetadata(
    resolved.context.studioName,
    "Studio",
    `How ${resolved.context.studioName} works, collaborates, and builds.`,
  );
}

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

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

  const hasStudioProse = Boolean(
    profile?.studioCulture ||
      profile?.workspace ||
      profile?.methodology ||
      profile?.collaborations,
  );

  return (
    <SiteShell
      studioName={resolved.context.studioName}
      location={profile?.location}
      active="studio"
      headerTone="dark"
    >
      <main>
        <SitePageHero
          tone="dark"
          eyebrow="Identity"
          title="Studio"
          description={`How ${resolved.context.studioName} works, collaborates, and builds.`}
        />

        {hasStudioProse ? (
          <section className="border-b border-[var(--archi-line)]">
            <div className="studio-container py-16 lg:py-20">
              <ProseBlock title="Culture" body={profile?.studioCulture} />
              <ProseBlock title="Workspace" body={profile?.workspace} />
              <ProseBlock title="Methodology" body={profile?.methodology} />
              <ProseBlock
                title="Collaborations"
                body={profile?.collaborations}
              />
            </div>
          </section>
        ) : (
          <section className="studio-container py-12 sm:py-16">
            <SiteEmptyState
              eyebrow="Studio notes"
              title="Culture and process"
              description={`${resolved.context.studioName} will publish culture, workspace, and methodology notes here.`}
              actionLabel="Contact the studio"
              actionHref="/contact"
            />
          </section>
        )}

        {team.length > 0 ? (
          <SiteSection
            eyebrow="People"
            title="Team"
            action={
              <SiteLink
                href="/team"
                className="text-sm text-[var(--archi-ink-muted)]"
              >
                View all →
              </SiteLink>
            }
          >
            <ul className="grid gap-8 sm:grid-cols-2 lg:grid-cols-3">
              {team.slice(0, 6).map((member) => (
                <li key={member.id}>
                  <SiteLink
                    href={`/team/${member.slug}`}
                    className="group block border-t border-[var(--archi-line)] pt-5"
                  >
                    <p className="font-[family-name:var(--font-display)] text-2xl transition group-hover:opacity-70">
                      {member.name}
                    </p>
                    {member.role ? (
                      <p className="mt-2 text-sm text-[var(--archi-ink-muted)]">
                        {member.role}
                      </p>
                    ) : null}
                  </SiteLink>
                </li>
              ))}
            </ul>
          </SiteSection>
        ) : null}

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