---
import type { PageByIdOutput } from '@boring.tools/schema'
import { Separator } from '@boring.tools/ui'
import type { z } from 'astro/zod'
import { format } from 'date-fns'
import { marked } from 'marked'

type PageById = z.infer<typeof PageByIdOutput>
const url = import.meta.env.DEV
  ? 'http://localhost:3000'
  : 'https://api.boring.tools'
const { id } = Astro.params
const response = await fetch(`${url}/v1/page/${id}/public`)
const data: PageById = await response.json()
---

<html lang="en">
	<head>
		<meta charset="utf-8" />
		<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
		<meta name="viewport" content="width=device-width" />
		<meta name="generator" content={Astro.generator} />
		<title>{data.title} | boring.tools</title>
	</head>
	<body class="bg-neutral-100">
		<div class="w-full flex items-center justify-center">
			<div class="w-full max-w-screen-sm flex my-6 flex-col gap-8">
				<div class="flex flex-col">
					<h1 class="prose prose-2xl">{data.title}</h1>
					<p class="prose text-sm">{data.description}</p>
				</div>

				{data.changelogs?.length >= 2 && <div class="flex flex-col">
					<h2 class="prose prose-xl">Changelogs</h2>
					{data.changelogs?.map((changelog) => {
						if (changelog.versions && changelog.versions?.length < 1) {
							return null
						}
						return <div>
							<h3 class="font-bold">{changelog.title}</h3>
							<Separator />
							<div class="flex flex-col gap-5 my-6">

							{changelog.versions?.map((version) => {
								return (
									<div class="flex gap-10 bg-white rounded p-3 border border-neutral-300">
										<div>
											<h2 class="prose text-3xl font-bold">
												{version.version}
											</h2>
											{version.releasedAt &&
											
											<p class="prose text-xs text-center">
												{format(new Date(String(version.releasedAt)), "dd-MM-yy")}
											</p>
											}
										</div>

											<div set:html={marked.parse(version.markdown ?? "")} />
									</div>
								)
							})}

							</div>
						</div>
					})}
					</div>}
				{data.changelogs?.length === 1 && <div>
<h2 class="uppercase text-sm prose tracking-widest">Changelog</h2>
{data.changelogs.map((changelog) => {
						if (changelog.versions && changelog.versions?.length < 1) {
							return null
						}
						return <div>
							<div class="flex flex-col gap-5 my-3">

							{changelog.versions?.map((version) => {
								return (
									<div class="flex gap-10 bg-white rounded p-3 border border-neutral-300">
										<div>
											<h2 class="prose text-3xl font-bold">
												{version.version}
											</h2>
											{version.releasedAt &&
											
											<p class="prose text-xs text-center">
												{format(new Date(String(version.releasedAt)), "dd-MM-yy")}
											</p>
											}
										</div>
										<div class="page" set:html={marked.parse(version.markdown ?? "")} />
									</div>
								)
							})}

							</div>
						</div>
					})}
					</div>}

			</div>
		</div>
	</body>
</html>
