import { NextRequest, NextResponse } from 'next/server'; import { getSession } from '@/lib/session'; import { triggerBackup } from '@/lib/backup'; import * as phpdb from '@/lib/phpdb'; export async function GET(request: NextRequest) { const session = await getSession(); if (!session) return NextResponse.json({ error: 'Nicht angemeldet' }, { status: 401 }); const { searchParams } = new URL(request.url); const kuppel = searchParams.get('kuppel') || 'West'; const limit = Math.min(parseInt(searchParams.get('limit') || '10') || 10, 500); const offset = Math.max(0, parseInt(searchParams.get('offset') || '0') || 0); const month = searchParams.get('month') || ''; const order = searchParams.get('order') === 'asc' ? 'asc' : 'desc'; const search = (searchParams.get('search') || '').trim(); try { const result = await phpdb.listLogbuch({ kuppel, limit, offset, month, search, order }); return NextResponse.json(result); } catch (error) { console.error('GET /api/logbuch:', error); return NextResponse.json({ error: 'Datenbankfehler' }, { status: 500 }); } } export async function POST(request: NextRequest) { const session = await getSession(); if (!session) return NextResponse.json({ error: 'Nicht angemeldet' }, { status: 401 }); try { const body = await request.json(); const { Kuppel, ArtFuehrung, SonderName, Spende, SpendeBetrag, Beginn, Ende, Besucher, beoIds, objekte, Bemerkungen, Wetter } = body; const result = await phpdb.createLogbuch({ Kuppel, ArtFuehrung, SonderName, Beginn, Ende, Spende: Spende ?? null, SpendeBetrag: SpendeBetrag ?? null, Besucher: Besucher ?? 0, beoIds: beoIds ?? [], objekte: objekte ?? [], Bemerkungen: Bemerkungen ?? null, Wetter: Wetter ?? null, created_by: session.beoId, }); triggerBackup(); // result enthält id und — bei Sonderführungen — den sofue-Status der Rückschreibung return NextResponse.json(result, { status: 201 }); } catch (error) { console.error('POST /api/logbuch:', error); return NextResponse.json({ error: 'Datenbankfehler' }, { status: 500 }); } }