import { NextResponse } from 'next/server'; import db from '@/lib/db'; export async function GET(req: Request) { const { searchParams } = new URL(req.url); const userId = searchParams.get('userId'); let isAdmin = false; if (userId) { const user = db.prepare('SELECT role FROM users WHERE id = ?').get(userId) as any; if (user && user.role === 'admin') isAdmin = true; } let logsQuery = 'SELECT * FROM logs ORDER BY created_at DESC'; if (!isAdmin) { logsQuery = 'SELECT * FROM logs WHERE is_secret = 0 ORDER BY created_at DESC'; } const logs = db.prepare(logsQuery).all(); const tech = db.prepare('SELECT * FROM tech_tree').all(); return NextResponse.json({ logs, tech, isAdmin }); } export async function POST(req: Request) { const body = await req.json(); const user = db.prepare('SELECT role FROM users WHERE id = ?').get(body.userId) as any; if (!user || user.role !== 'admin') { return NextResponse.json({ error: "Unauthorized" }, { status: 403 }); } if (body.type === 'LOG') { db.prepare('INSERT INTO logs (title, content, is_secret, game_date) VALUES (?, ?, ?, ?)').run( body.title, body.content, body.is_secret ? 1 : 0, body.game_date ); } if (body.type === 'TECH') { const exists = db.prepare('SELECT id FROM tech_tree WHERE name = ?').get(body.name); if (exists) { db.prepare('UPDATE tech_tree SET progress = ?, status = ? WHERE name = ?').run(body.progress, body.status, body.name); } else { db.prepare('INSERT INTO tech_tree (name, progress, status) VALUES (?, ?, ?)').run(body.name, body.progress, body.status); } } return NextResponse.json({ success: true }); }