import { db } from "./db";
import { cookies } from "next/headers";

export async function getUserFromSession() {
    const sessionId = (await cookies()).get('session')?.value;

    if (!sessionId) {
        return null;
    }

    const [rows] = await (await db).query('SELECT * FROM sessions WHERE SESSION_ID = ?', [sessionId]);
    if (rows.length === 0) {
        return null;
    }

    const [userRows] = await (await db).query('SELECT * FROM users WHERE USER_ID = ?', [rows[0].USER_ID]);

    
    return { user: userRows.length ? userRows[0] : null, session: rows.length ? rows[0] : null };
}