import { getSession } from '@/lib/session'
import { getPost, updatePost, setPostDeleted } from '@/lib/forum-write'
import { getTopic } from '@/lib/forum'
import { forumIsModerator, canEditPost } from '@/lib/forum-perm'
import { plainLength } from '@/lib/forum-sanitize'

const MIN_TEXT_LEN = 5

/** Restaurar un post borrado. Solo moderadores. */
export async function PUT(request: Request) {
  const session = await getSession()
  if (!session.accountId) return Response.json({ success: false, error: 'notAuthenticated' }, { status: 401 })
  if (!(await forumIsModerator(session))) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })

  let b: Record<string, string> = {}
  try { b = await request.json() } catch { return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 }) }
  const postId = Number(b.postId)
  if (!postId) return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 })

  await setPostDeleted(postId, false)
  return Response.json({ success: true })
}

/** Editar el texto de un post propio (o cualquiera si es moderador). */
export async function PATCH(request: Request) {
  const session = await getSession()
  if (!session.accountId) return Response.json({ success: false, error: 'notAuthenticated' }, { status: 401 })

  let b: Record<string, string> = {}
  try { b = await request.json() } catch { return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 }) }
  const postId = Number(b.postId)
  const text = String(b.text ?? '').trim()
  if (!postId) return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 })

  const post = await getPost(postId)
  if (!post) return Response.json({ success: false, error: 'postNotFound' }, { status: 404 })

  const isMod = await forumIsModerator(session)
  if (!canEditPost(session, isMod, post.poster_id)) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })

  const topic = await getTopic(post.topic_id)
  if (topic?.locked && !isMod) return Response.json({ success: false, error: 'topicLocked' })
  if (plainLength(text) < MIN_TEXT_LEN) return Response.json({ success: false, error: 'tooShort' })

  await updatePost(postId, text)
  return Response.json({ success: true })
}

/** Borrar (lógico) un post propio (o cualquiera si es moderador). */
export async function DELETE(request: Request) {
  const session = await getSession()
  if (!session.accountId) return Response.json({ success: false, error: 'notAuthenticated' }, { status: 401 })

  let b: Record<string, string> = {}
  try { b = await request.json() } catch { return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 }) }
  const postId = Number(b.postId)
  if (!postId) return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 })

  const post = await getPost(postId)
  if (!post) return Response.json({ success: false, error: 'postNotFound' }, { status: 404 })

  const isMod = await forumIsModerator(session)
  if (!canEditPost(session, isMod, post.poster_id)) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })

  const topic = await getTopic(post.topic_id)
  if (topic?.locked && !isMod) return Response.json({ success: false, error: 'topicLocked' })

  await setPostDeleted(postId, true)
  return Response.json({ success: true })
}
