---
import { Image } from "astro:assets";
import { getCollection } from "astro:content";
import type { ImageMetadata } from "astro";
import profilePicture from "@/assets/images/intro.jpg";
import BaseLayout from "@/layouts/BaseLayout.astro";
import {
  buildCollectionPhotos,
  collectionSlug,
  type PhotoSidecar,
} from "@/lib/collections";

const articles = (await getCollection("blog", ({ data }) => !data.draft))
  .sort((a, b) => b.data.publishDate.valueOf() - a.data.publishDate.valueOf())
  .slice(0, 3);

const notes = (await getCollection("notes", ({ data }) => !data.draft))
  .sort((a, b) => b.data.publishDate.valueOf() - a.data.publishDate.valueOf())
  .slice(0, 3);

const allCollections = await getCollection(
  "collections_photos",
  ({ data }) => !data.draft,
);
const sidecars = import.meta.glob<PhotoSidecar>(
  "/src/content/photos/collections/**/*.json",
  { eager: true },
);
const imageModules = import.meta.glob<{ default: ImageMetadata }>(
  "/src/content/photos/collections/**/*.jpg",
  { eager: true },
);

const photoEntries = allCollections
  .flatMap((col) => {
    const slug = collectionSlug(col);
    return buildCollectionPhotos(sidecars, imageModules, slug).map((p) => ({
      image: p.image,
      href: `/photos/collections/${slug}/${p.sidecar.id}`,
      date: p.sidecar.date,
    }));
  })
  .sort((a, b) => a.date.localeCompare(b.date))
  .slice(-6);

function formatDate(date: Date) {
  return date.toLocaleDateString("de-DE", {
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
  });
}
---

<BaseLayout
  title="Adrian Altner"
  description="Longtime web enthusiast with a strong interest in technology who enjoys photography, travel, and learning new things in his spare time."
  navDesktopHidden={true}
>
  <div class="page">
    <!-- Hero -->
    <main class="hero">
      <div class="hero__card h-card">
        <div class="hero__photo-wrap">
          <Image
            src={profilePicture}
            alt="Adrian Altner"
            class="hero__photo u-photo"
            width={200}
            height={200}
          />
        </div>
        <div class="hero__text">
          <h1 class="hero__name">
            <a href="https://adrian-altner.com" class="u-url p-name"
              >Adrian Altner</a>
          </h1>
          <p class="hero__intro p-note">
            I'm a longtime <strong>web enthusiast</strong> with a strong interest
            in
            <strong>technology</strong>. In my spare time, I enjoy photography,
            traveling, and learning new things on my own.
          </p>
        </div>
      </div>
      <div class="hero__divider" aria-hidden="true"></div>
      <nav class="hero__nav" aria-label="Main navigation">
        <a href="/blog">Blog</a>
        <a href="/photos">Photos</a>
        <a href="/notes">Notes</a>
        <a href="/links">Links</a>
        <a href="/archives">Archives</a>
        <a href="/about">About</a>
      </nav>
    </main>

    <!-- Content columns -->
    <section class="content-grid">
      <div class="col">
        <h2 class="col__heading"><a href="/blog">Blog</a></h2>
        <ul class="preview-list">
          {
            articles.map((a) => (
              <li class="preview-item">
                <time
                  class="preview-item__date"
                  datetime={a.data.publishDate.toISOString()}
                >
                  {formatDate(a.data.publishDate)}
                </time>
                <a href={`/blog/${a.id}`} class="preview-item__title">
                  {a.data.title}
                </a>
                <p class="preview-item__desc">{a.data.description}</p>
              </li>
            ))
          }
        </ul>
      </div>

      <div class="col">
        <h2 class="col__heading"><a href="/notes">Notes</a></h2>
        <ul class="preview-list">
          {
            notes.map((n) => (
              <li class="preview-item">
                <time
                  class="preview-item__date"
                  datetime={n.data.publishDate.toISOString()}
                >
                  {formatDate(n.data.publishDate)}
                </time>
                <a href={`/notes/${n.id}`} class="preview-item__title">
                  {n.data.title}
                </a>
                {n.data.description && (
                  <p class="preview-item__desc">{n.data.description}</p>
                )}
              </li>
            ))
          }
        </ul>
      </div>
    </section>

    <!-- Photo stream -->
    {
      photoEntries.length > 0 && (
        <section class="photo-stream">
          <h2 class="photo-stream__heading">
            Last photos from <a href="/photos/collections">my collections</a>
          </h2>
          {photoEntries.map((entry) => (
            <a href={entry.href} class="photo-stream__item">
              <Image
                src={entry.image}
                alt=""
                class="photo-stream__img"
                width={400}
                height={300}
              />
            </a>
          ))}
        </section>
      )
    }
  </div>
</BaseLayout>

<style>
  .page {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    min-height: 100dvh;
    color: #111;
  }

  /* ── Hero ── */
  .hero {
    max-width: var(--width-hero);
    margin: 0 auto;
    width: 100%;
    padding: 5rem 2rem 6rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0;
  }

  .hero__divider {
    width: 1px;
    align-self: stretch;
    background: #e0e0de;
    margin: 0 2rem;
    flex-shrink: 0;
  }

  .hero__card {
    display: flex;
    align-items: center;
    max-width: 60ch;
    gap: 1.5rem;
  }

  .hero__nav {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.4rem 0;
    flex-shrink: 0;
  }

  .hero__nav a {
    font-size: var(--text-xs);
    font-weight: 500;
    color: #111;
    text-decoration: none;
    padding: 0.3rem 0.75rem;
    border-radius: 4px;
    text-align: left;
    transition: background 0.15s;
  }

  .hero__nav a:hover {
    background: #e8e8e6;
  }

  .hero__photo-wrap {
    width: 160px;
    height: 160px;
    border-radius: 50%;
    overflow: hidden;
    flex-shrink: 0;
  }

  .hero__photo {
    width: 156px;
    height: 156px;
    object-fit: cover;
    transform: scale(1.25);
  }

  .hero__name a {
    color: inherit;
    text-decoration: none;
  }

  .hero__name {
    font-size: var(--text-xl);
    font-weight: 400;
    letter-spacing: -0.02em;
    line-height: 1.1;
    margin-bottom: 0.5rem;
  }

  .hero__intro {
    font-size: var(--text-xs);
    line-height: 1.35;
    color: #444;
    max-width: 46ch;
  }

  .hero__intro strong {
    font-weight: 700;
    color: #111;
  }

  /* ── Content grid ── */
  .content-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    max-width: var(--width-hero);
    margin: 0 auto;
    padding: 0 2rem 3rem;
    width: 100%;
  }

  .col__heading {
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: #888;
    margin-bottom: 1rem;
  }

  .col__heading a {
    color: inherit;
    text-decoration: none;
  }

  .col__heading a:hover {
    color: #111;
  }

  .preview-list {
    list-style: none;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 0;
  }

  .preview-item {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
    padding: 0.75rem 0;
    border-bottom: 1px solid #e0e0de;
  }

  .preview-item:first-child {
    border-top: 1px solid #e0e0de;
  }

  .preview-item__date {
    color: #999;
  }

  .preview-item__title {
    font-size: var(--text-xs);
    font-weight: 500;
    color: #111;
    text-decoration: none;
    line-height: 1.3;
  }

  .preview-item__title:hover {
    color: var(--accent);
  }

  .preview-item__desc {
    color: #666;
    line-height: 1.4;
    margin: 0;
  }

  /* ── Photo stream ── */
  .photo-stream {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
    max-width: var(--width-hero);
    margin: 0 auto;
    padding: 0 2rem 4rem;
    width: 100%;
  }

  .photo-stream__heading {
    grid-column: 1 / -1;
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: #888;
    margin-bottom: 0.25rem;
  }

  .photo-stream__heading a {
    color: inherit;
    text-decoration: underline;
  }

  .photo-stream__heading a:hover {
    color: var(--accent);
  }

  .photo-stream__item {
    display: block;
    aspect-ratio: 4 / 3;
    overflow: hidden;
    cursor: pointer;
  }

  .photo-stream__item:hover .photo-stream__img {
    transform: scale(1.04);
    transition: transform 0.3s ease;
  }

  .photo-stream__img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    transition: transform 0.3s ease;
  }

  /* ── Mobile breakpoint ── */
  @media (max-width: 640px) {
    .hero {
      flex-direction: column;
      align-items: flex-start;
      padding-top: 2rem;
      padding-bottom: 3rem;
    }

    /* flex-wrap: photo + name centered in row 1, intro wraps to row 2 */
    .hero__card {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      justify-content: center;
      gap: 0.25rem 1rem;
      width: 100%;
    }

    .hero__text {
      display: contents;
    }

    .hero__photo-wrap {
      width: 96px;
      height: 96px;
      flex-shrink: 0;
    }

    .hero__photo {
      width: 92px;
      height: 92px;
    }

    .hero__name {
      min-width: 0;
      overflow-wrap: break-word;
      margin-bottom: 0;
    }

    .hero__intro {
      flex-basis: 100%;
      margin-top: 0.75rem;
      text-align: center;
    }

    .hero__nav {
      display: none;
    }

    .content-grid {
      grid-template-columns: 1fr;
      gap: 2rem;
    }

    .photo-stream {
      grid-template-columns: repeat(2, 1fr);
    }
  }
</style>
