---
import { getCollection } from 'astro:content'
import TaShowcaseLayout from '../../layouts/taShowcaseLayout.astro'
import Layout from '../../layouts/Layout.astro'
import DatabaseSection from '../../layouts/databaseSection.astro'
import QuestListing from '../../components/listings/questListingCard.astro'
import SmallTitle from '../../components/smallTitle.astro'

interface store {
  slug: string
  group: boolean
  questinfo:
    | undefined
    | {
        slug: string
        info: {
          title: string
          releaseDate: Date
          shortdescription: string
          releaseNumber: number
        }
      }[]
  pageinfo:
    | undefined
    | {
        title: string
        shortdescription: string
      }
}

export async function getStaticPaths() {
  const fulldata = await getCollection('taInfoData')
  const slugdata: store[] = fulldata.map((data) => {
    return {
      slug: data.id,
      group: false,
      questinfo: undefined,
      pageinfo: undefined,
    }
  })

  const groupdata = await getCollection('groups')
  const groups: store[] = groupdata.map((data) => {
    return {
      slug: data.id,
      group: true,
      questinfo: undefined,
      pageinfo: undefined,
    }
  })

  const full = slugdata.concat(groups as any)
  full.map((data) => {
    if (data.group) {
      const coll = fulldata.filter((d) => d.id.split("/", 1)[0] == data.slug)
      data.questinfo = coll.map((d) => {
        return { slug: d.id, info: d.data.info }
      })
      data.questinfo.sort(
        (a, b) =>
          b.info.releaseDate.valueOf() - a.info.releaseDate.valueOf() ||
          b.info.releaseNumber - a.info.releaseNumber
      )
      const page = groupdata.find((d) => d.id === data.slug)
      data.pageinfo = page?.data.info
    }
  })

  return full.map((data) => ({
    params: { slug: data.slug },
    props: {
      key: data.slug,
      group: data.group,
      questinfo: data.questinfo,
      pageinfo: data.pageinfo,
    },
  }))
}

const { key, group, questinfo, pageinfo } = Astro.props

let baseurl_return = "database"
if (key.includes("/")) {
  baseurl_return = baseurl_return + "/" + key.split("/", 1)[0]
}

---

{
  !group && (
    <TaShowcaseLayout
      collection="taInfoData"
      collectionKey={key}
      baseurl={baseurl_return}
    />
  )
}
{
  group && (
    <Layout
      title={`${pageinfo!.title} - TA Database - FGO TA`}
      currentpage="database"
      descriptionOverride={pageinfo!.shortdescription}
    >
      <SmallTitle
        maintext={pageinfo!.title}
        subtext={pageinfo!.shortdescription}
        fadeout={true}
        baseurl="database"
        returnbutton={true}
      />
      <DatabaseSection title="" titlehidden={true}>
        {questinfo!.map((quest) => (
          <QuestListing
            {...{ ...quest.info, slug: quest.slug }}
            baseurl="database"
          />
        ))}
      </DatabaseSection>
    </Layout>
  )
}
