import { NextResponse } from 'next/server' import { getServerSession } from 'next-auth' import { authOptions } from '@/lib/auth' export async function GET() { const session = await getServerSession(authOptions) if (!session) return NextResponse.json({ error: 'Non authentifié' }, { status: 401 }) const apiKey = process.env.GOOGLE_API_KEY const sheetId = process.env.GOOGLE_SHEETS_ID if (!apiKey || !sheetId) { return NextResponse.json({ error: 'GOOGLE_API_KEY et GOOGLE_SHEETS_ID requis' }, { status: 500 }) } try { const url = `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}?key=${apiKey}&fields=sheets.properties.title` const res = await fetch(url, { cache: 'no-store' }) if (!res.ok) { const err = await res.text() throw new Error(`Google Sheets API error: ${res.status} — ${err}`) } const data = await res.json() const tabs: string[] = (data.sheets as Array<{ properties: { title: string } }>).map( (s) => s.properties.title, ) return NextResponse.json({ tabs }) } catch (err) { const message = err instanceof Error ? err.message : 'Erreur inconnue' return NextResponse.json({ error: message }, { status: 500 }) } }