import { NextRequest, NextResponse } from 'next/server' import { requireAdmin } from '@/lib/admin-auth' import { createServiceClient } from '@/lib/supabase' const BUCKET = 'site-assets' export async function POST(req: NextRequest) { const check = await requireAdmin() if (check instanceof NextResponse) return check const db = createServiceClient() const formData = await req.formData() const file = formData.get('file') as File if (!file) return NextResponse.json({ error: 'file erforderlich' }, { status: 400 }) const ext = file.name.split('.').pop()?.toLowerCase() ?? 'png' const path = `logo-${Date.now()}.${ext}` const { data: existing } = await db.from('hero_content').select('logo_path').limit(1).single() if (existing?.logo_path) { await db.storage.from(BUCKET).remove([existing.logo_path]) } const { error: uploadError } = await db.storage.from(BUCKET).upload(path, file, { contentType: file.type, upsert: true }) if (uploadError) return NextResponse.json({ error: uploadError.message }, { status: 500 }) const { data: row } = await db.from('hero_content').select('id').limit(1).single() const { error } = row ? await db.from('hero_content').update({ logo_path: path }).eq('id', row.id) : await db.from('hero_content').insert({ logo_path: path }) if (error) return NextResponse.json({ error: error.message }, { status: 500 }) return NextResponse.json({ path }) } export async function DELETE() { const check = await requireAdmin() if (check instanceof NextResponse) return check const db = createServiceClient() const { data: existing } = await db.from('hero_content').select('id, logo_path').limit(1).single() if (existing?.logo_path) { await db.storage.from(BUCKET).remove([existing.logo_path]) } if (existing?.id) { await db.from('hero_content').update({ logo_path: null }).eq('id', existing.id) } return NextResponse.json({ success: true }) }