import dotenv from 'dotenv';
dotenv.config();

import type { VercelRequest, VercelResponse } from '@vercel/node';
import app from '../src/server/app.js';
import { connectDB } from '../src/server/config/database.js';

export default async function handler(req: VercelRequest, res: VercelResponse) {
    try {
        console.log('--- API CALL:', req.url);

        // Conecta ao Banco de Dados (Supabase/Postgres)
        await connectDB();

        // Passa o controle para o Express
        return app(req, res);
    } catch (error: unknown) {
        console.error('SERVERLESS BOOT ERROR:', error);
        const message = error instanceof Error ? error.message : 'Unknown error';
        return res.status(500).json({
            error: 'Serverless Boot Error',
            message: message,
            path: req.url
        });
    }
}
