import { NextResponse } from 'next/server'
import { getDb } from '@/lib/db'
import { seedIfEmpty } from '@/lib/seed'

export async function GET() {
  seedIfEmpty()
  const db = getDb()
  const items = db.prepare('SELECT * FROM board_versions ORDER BY id').all()
  return NextResponse.json(items)
}

export async function POST(req: Request) {
  seedIfEmpty()
  const db = getDb()
  const body = await req.json()
  const { type, version, status } = body
  const result = db.prepare('INSERT INTO board_versions (type, version, status) VALUES (?, ?, ?)').run(type, version, status || '在产')
  return NextResponse.json({ id: result.lastInsertRowid })
}

export async function PUT(req: Request) {
  seedIfEmpty()
  const db = getDb()
  const body = await req.json()
  const { id, status } = body
  db.prepare('UPDATE board_versions SET status = ? WHERE id = ?').run(status, id)
  return NextResponse.json({ ok: true })
}
