import { getStore } from "@netlify/blobs"

import { env } from "@/env/server"

import type { SitemapData, SyncItem } from "@/types/sitemap"

const branch = env.CMS_BRANCH
const environment = env.CMS_ENVIRONMENT
const entriesKey = `${environment}/${branch}/entries`
const syncTokenKey = `${environment}/${branch}/syncToken`
const sitemapDataKey = `${environment}/${branch}/sitemapData`
const lastUpdatedKey = `${environment}/${branch}/lastUpdated`
const MAX_ENTRIES_PER_SITEMAP = 50000

// We need to wrap `getStore` because calling it in the root of the file causes
// it to be executed during build time. This is not supported by Netlify.
function store() {
  return getStore("sitemap")
}

export async function saveEntries(entries: SyncItem[]) {
  await store().setJSON(entriesKey, entries)
}

export async function saveSitemapData(sitemapData: SitemapData) {
  await store().setJSON(sitemapDataKey, sitemapData)
}

export async function saveLastUpdatedDate(lastUpdated: string) {
  await store().set(lastUpdatedKey, lastUpdated)
}

export async function saveSyncToken(syncToken: string) {
  await store().set(syncTokenKey, syncToken)
}

export async function getSyncToken() {
  return await store().get(syncTokenKey)
}

export async function getEntries() {
  const entries: SyncItem[] = await store().get(entriesKey, {
    type: "json",
  })

  return entries || []
}

export async function getLastUpdated() {
  return await store().get(lastUpdatedKey)
}

export async function getSitemapData() {
  const sitemapData: SitemapData | null = await store().get(sitemapDataKey, {
    type: "json",
  })

  return sitemapData || []
}

export async function getSitemapDataById(id: number) {
  const sitemapData = await getSitemapData()
  const index = id - 1
  const start = index * MAX_ENTRIES_PER_SITEMAP
  const end = start + MAX_ENTRIES_PER_SITEMAP
  return sitemapData.slice(start, end)
}

export async function getSitemapIds() {
  const sitemapData = await getSitemapData()
  const numberOfSitemaps = Math.ceil(
    sitemapData.length / MAX_ENTRIES_PER_SITEMAP
  )
  return Array.from({ length: numberOfSitemaps }, (_, index) => index + 1)
}
