
import { NextResponse } from 'next/server';
import db from '@/db';

export async function GET() {
  const libraries = db.prepare('SELECT * FROM libraries').all();
  return NextResponse.json(libraries);
}

export async function POST(request: Request) {
  const { path } = await request.json();
  if (!path) {
    return NextResponse.json({ error: 'Path is required' }, { status: 400 });
  }

  try {
    const info = db.prepare('INSERT INTO libraries (path) VALUES (?)').run(path);
    return NextResponse.json({ id: info.lastInsertRowid, path });
  } catch (error: any) {
    if (error.code === 'SQLITE_CONSTRAINT_UNIQUE') {
      return NextResponse.json({ error: 'Path already exists' }, { status: 409 });
    }
    return NextResponse.json({ error: error.message }, { status: 500 });
  }
}
