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 licenses ORDER BY id').all()
  return NextResponse.json(items)
}

export async function POST(req: Request) {
  seedIfEmpty()
  const db = getDb()
  const body = await req.json()
  const { model, modules, expiry, status } = body
  const result = db.prepare('INSERT INTO licenses (model, modules, expiry, status) VALUES (?, ?, ?, ?)').run(model, modules, expiry || '', status || '生效')
  return NextResponse.json({ id: result.lastInsertRowid })
}
