import WikiArticle from '../models/wikiArticle.js'
import { connectDB } from './mongoose.js'
import { fetchAllDocuments, fetchCollections, extractSummary } from './outline.js'

export async function syncWikiArticles() {
  const [documents, collectionMap] = await Promise.all([
    fetchAllDocuments(),
    fetchCollections()
  ])

  await connectDB()

  const fetchedOutlineIds = new Set(documents.map((doc) => doc.id))

  const existing = await WikiArticle.find({}, 'outlineId publishedAt')
  const existingByOutlineId = new Map(
    existing.map((a) => [a.outlineId, a])
  )

  let created = 0
  let updated = 0
  let deleted = 0
  let errors = 0

  for (const doc of documents) {
    try {
      // Only $set fields from Outline — tags are never touched
      const articleData = {
        title: doc.title,
        collection: collectionMap.get(doc.collectionId) || null,
        url: doc.url?.startsWith('/') ? `https://wiki.ghostguild.org${doc.url}` : doc.url,
        summary: extractSummary(doc.text),
        publishedAt: doc.publishedAt ? new Date(doc.publishedAt) : new Date(doc.createdAt),
        permission: doc.permission || null,
        lastSyncedAt: new Date(),
        outlineUpdatedAt: doc.updatedAt ? new Date(doc.updatedAt) : null
      }

      const result = await WikiArticle.findOneAndUpdate(
        { outlineId: doc.id },
        { $set: articleData },
        { upsert: true, new: true, rawResult: true }
      )

      if (result.lastErrorObject?.updatedExisting) {
        updated++
      } else {
        created++
      }
    } catch (err) {
      console.error(`[wiki-sync] Error upserting doc ${doc.id}:`, err)
      errors++
    }
  }

  for (const [outlineId, article] of existingByOutlineId) {
    if (!fetchedOutlineIds.has(outlineId) && article.publishedAt !== null) {
      try {
        await WikiArticle.findOneAndUpdate(
          { outlineId },
          { $set: { publishedAt: null, lastSyncedAt: new Date() } }
        )
        deleted++
      } catch (err) {
        console.error(`[wiki-sync] Error soft-deleting ${outlineId}:`, err)
        errors++
      }
    }
  }

  return { created, updated, deleted, errors }
}
