---
import { SITE } from '@/consts'
import { isSubpost } from '@/lib/data-utils'
import { buildNotionApiUrl } from '@/lib/notion/api'
import type { CollectionEntry } from 'astro:content'

interface Props {
  post: CollectionEntry<'blog'>
}

const { post } = Astro.props

const title = post.data.title || SITE.title
const description = post.data.description || SITE.description
const siteUrl = Astro.site ?? new URL(SITE.href)
const author =
  post.data.authors && post.data.authors.length > 0
    ? post.data.authors.join(', ')
    : SITE.author
const ogImage = {
  url: buildNotionApiUrl(`/og/${encodeURIComponent(post.id)}.png`),
  width: 1200,
  height: 630,
  type: 'image/png',
}
const canonicalUrl = Astro.url
---

<title>{`${title} | ${SITE.title}`}</title>
<meta name="title" content={`${title} | ${SITE.title}`} />
<meta name="description" content={description} />
<link rel="canonical" href={canonicalUrl} />

{isSubpost(post.id) && <meta name="robots" content="noindex" />}

<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:image" content={ogImage.url} />
<meta property="og:image:alt" content={title} />
{
  ogImage.width && (
    <meta property="og:image:width" content={String(ogImage.width)} />
  )
}
{
  ogImage.height && (
    <meta property="og:image:height" content={String(ogImage.height)} />
  )
}
{ogImage.type && <meta property="og:image:type" content={ogImage.type} />}
<meta property="og:type" content="article" />
<meta property="og:locale" content={SITE.locale} />
<meta property="og:site_name" content={SITE.title} />
<meta property="og:url" content={canonicalUrl} />
<meta property="og:author" content={author} />
<meta
  property="article:published_time"
  content={post.data.date.toISOString()}
/>

<meta name="twitter:title" content={title} />
<meta name="twitter:description" content={description} />
<meta name="twitter:image" content={ogImage.url} />
<meta name="twitter:image:alt" content={title} />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:creator" content={author} />

{
  post?.data.tags &&
    post.data.tags.map((tag: string) => {
      return <meta property="article:tag" content={tag} />
    })
}
