// app/api/debug/env-check/route.ts
export const dynamic = "force-dynamic";
import { NextResponse } from "next/server";

/**
 * Route de debug pour vérifier les variables d'environnement critiques
 * NE PAS UTILISER EN PRODUCTION (sauf temporairement)
 */
export async function GET() {
  const envCheck = {
    // Variables publiques
    NEXT_PUBLIC_SUPABASE_URL: {
      exists: !!process.env.NEXT_PUBLIC_SUPABASE_URL,
      value: process.env.NEXT_PUBLIC_SUPABASE_URL ? `${process.env.NEXT_PUBLIC_SUPABASE_URL.substring(0, 30)}...` : 'MANQUANT',
    },
    NEXT_PUBLIC_SUPABASE_ANON_KEY: {
      exists: !!process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
      length: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY?.length || 0,
      preview: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY ? `${process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY.substring(0, 20)}...` : 'MANQUANT',
    },
    NEXT_PUBLIC_SITE_URL: {
      exists: !!process.env.NEXT_PUBLIC_SITE_URL,
      value: process.env.NEXT_PUBLIC_SITE_URL || 'MANQUANT',
    },
    
    // Variables privées (ne pas afficher les valeurs)
    SUPABASE_SERVICE_ROLE_KEY: {
      exists: !!process.env.SUPABASE_SERVICE_ROLE_KEY,
      length: process.env.SUPABASE_SERVICE_ROLE_KEY?.length || 0,
    },
    SUPABASE_URL: {
      exists: !!process.env.SUPABASE_URL,
      value: process.env.SUPABASE_URL ? `${process.env.SUPABASE_URL.substring(0, 30)}...` : 'MANQUANT',
    },
    
    // Environnement
    NODE_ENV: process.env.NODE_ENV || 'MANQUANT',
    VERCEL_URL: process.env.VERCEL_URL || 'non défini (normal sur Coolify)',
    
    // Résumé
    summary: {
      allPublicVarsPresent: !!(
        process.env.NEXT_PUBLIC_SUPABASE_URL && 
        process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY &&
        process.env.NEXT_PUBLIC_SITE_URL
      ),
      allPrivateVarsPresent: !!(
        process.env.SUPABASE_SERVICE_ROLE_KEY &&
        process.env.SUPABASE_URL
      ),
    }
  };
  
  return NextResponse.json(envCheck, { 
    status: 200,
    headers: {
      'Cache-Control': 'no-store, no-cache, must-revalidate',
    }
  });
}
