import { NextApiRequest, NextApiResponse } from "next";
import { IncomingMessage, ServerResponse } from "http";
import { deleteCookie } from "../../utils/cookies-actions";
import { APIResponseObject } from "@moduletrace/datasquirel/dist/package-shared/types";
import grabCookieNames from "../../utils/grab-cookie-names";

type Params = {
    res: NextApiResponse | ServerResponse;
    req: NextApiRequest | IncomingMessage;
};

export default async function logoutUser({
    res,
    req,
}: Params): Promise<APIResponseObject> {
    const { auth_key_cookie_name, csrf_cookie_name } = grabCookieNames();

    const host = req.headers.host;

    deleteCookie(res, [
        {
            name: auth_key_cookie_name,
            options: {
                path: "/",
                domain: host,
            },
        },
        {
            name: csrf_cookie_name,
            options: {
                path: "/",
                domain: host,
            },
        },
    ]);

    return {
        success: true,
    };
}
