---
import type { CollectionEntry } from 'astro:content';
import { render } from 'astro:content';
import Base from '@layouts/Base.astro';
import Header from '@compontents/layout/PostHeader/index.astro';
import { getBreadcrumbs } from '@lib/utils/paths';
import { toMilitaryDTG } from '@lib/utils/date';

interface Props {
  entry: CollectionEntry<'articles'> | CollectionEntry<'elements'>;
  collectionName: string;
}

const { entry, collectionName } = Astro.props;
const { Content } = await render(entry);
const { title, summary, subtitle, updateDate, publishDate, tags, seo } =
  entry.data;
const hasMargin = Astro.slots.has('margin')

const breadcrumbs = await getBreadcrumbs(
  entry.data.parent ?? null,
  collectionName,
);
const formattedPublishDate = toMilitaryDTG(publishDate);
const formattedUpdateDate = updateDate
  ? toMilitaryDTG(updateDate)
  : formattedPublishDate;
const rawCover = entry.data.cover;
const headerCover =
  rawCover?.src && rawCover?.showInHeader
    ? {
        src: rawCover.src,
        alt: rawCover.alt ?? '',
        caption: rawCover.caption ?? '',
        showInHeader: rawCover.showInHeader,
      }
    : undefined;
---

<Base title={title} seo={seo}>
  <main>
    <article>
      <Header
        title={title}
        breadcrumbs={breadcrumbs}
        publishDate={formattedPublishDate}
        updateDate={formattedUpdateDate}
        cover={headerCover}
        tags={tags}
        subtitle={subtitle}
      />
      <div class="frame">
        <div class="body content prose">
          <slot name="before-content" />
          <Content />
          <slot name="after-content" />
        </div>
        {hasMargin &&(
          <aside class="margin">
            <slot name="margin" />
          </aside>
        )}
      </div>
    </article>
  </main>
</Base>

<style>
  .frame {
    @mixin layout-wrapper;

    @media (--bp-desktop) {
      position: relative;
      display: grid;
      grid-template-columns: var(--layout-content-width) var(--layout-margin-width);
      gap: var(--layout-gutter);
    }
  }

  .body {
    min-width: 0;
  }

  .margin {
    position: relative;
    min-width: 0;
  }
</style>
