import { NextResponse } from 'next/server';
import { serialize } from 'cookie';
import { db } from '@/app/lib/db';
import { cookies } from 'next/headers';

export async function POST() {
    const session_id = (await cookies()).get('session');
    const [rows] = await (await db).query('DELETE FROM sessions WHERE SESSION_ID = ?', [session_id.value]);

    if (rows.affectedRows === 0) {
        return NextResponse.json({ message: 'No session with the provided ID' }, { status: 500 });
    }

    const response = NextResponse.json({ message: 'OK' });
    response.headers.set('Set-Cookie', serialize('session', '', {
        httpOnly: true,
        secure: process.env.NODE_ENV === 'production',
        maxAge: -1,
        path: '/',
    }));
    return response;
}