import { notFound, redirect } from 'next/navigation'
import { getTranslations, setRequestLocale } from 'next-intl/server'
import { getForum, getForumPath } from '@/lib/forum'
import { getSession } from '@/lib/session'
import { PageShell } from '@/components/PageShell'
import { ForumBreadcrumb } from '@/components/ForumBreadcrumb'
import { CreateTopicForm } from '@/components/CreateTopicForm'

export const dynamic = 'force-dynamic'

export default async function CreateTopicPage({
  params,
}: {
  params: Promise<{ locale: string; forumId: string }>
}) {
  const { locale, forumId } = await params
  setRequestLocale(locale)
  const t = await getTranslations('Forum')

  const id = Number(forumId)
  const forum = await getForum(id)
  if (!forum) notFound()

  const session = await getSession()
  if (!session.username) redirect(`/${locale}/forum/${id}`)

  const path = await getForumPath(id)

  return (
    <PageShell title={t('createTopic')}>
      <div className="main-wide">
        {path && (
          <ForumBreadcrumb
            items={[
              { label: t('title'), href: '/forum' },
              { label: path.forum, href: `/forum/${id}` },
              { label: t('createTopic') },
            ]}
          />
        )}
        <div className="forum_header create_header">
          <div className="forum_title">
            <h1>{forum.name}</h1>
            {forum.description && <h3>{forum.description}</h3>}
          </div>
        </div>
        <CreateTopicForm forumId={id} />
      </div>
    </PageShell>
  )
}
