import axios from "axios";
import { type MiddlewareHandler } from "hono";
import jwt from "jsonwebtoken";

const { sign, verify } = jwt;

export const authMiddleware: MiddlewareHandler = async (c, next) => {
	console.log("middleware checked");
	// const cookieHeader = c.req.header("Cookie");
	// if (!cookieHeader) return c.json({ error: "Unauthorized" }, 401);

	// const res = await axios.get(`${process.env.LST_BASE_URL}/api/user/me`, {
	// 	headers: { Cookie: cookieHeader },
	// });

	// if (res.status === 401) return c.json({ error: "Unauthorized" }, 401);

	// //const user = await resp.json();
	// c.set("user", res.data.user);
	return next();
};

// export const authMiddleware: MiddlewareHandler = async (c, next) => {
//     const authHeader = c.req.header("Authorization");

//     if (!authHeader || !authHeader.startsWith("Bearer ")) {
//         return c.json({error: "Unauthorized"}, 401);
//     }

//     const token = authHeader.split(" ")[1];

//     try {
//         const decoded = verify(token, process.env.JWT_SECRET!, {ignoreExpiration: false}) as {
//             userId: number;
//             exp: number;
//         };

//         const currentTime = Math.floor(Date.now() / 1000); // Get current timestamp
//         const timeLeft = decoded.exp - currentTime;

//         // If the token has less than REFRESH_THRESHOLD seconds left, refresh it
//         let newToken = null;

//         if (timeLeft < parseInt(process.env.REFRESH_THRESHOLD!)) {
//             newToken = sign({userId: decoded.userId}, process.env.JWT_SECRET!, {
//                 expiresIn: parseInt(process.env.EXPIRATION_TIME!),
//             });
//             c.res.headers.set("Authorization", `Bearer ${newToken}`);
//         }

//         c.set("user", {id: decoded.userId});
//         await next();

//         // If a new token was generated, send it in response headers
//         if (newToken) {
//             console.log("token was refreshed");
//             c.res.headers.set("X-Refreshed-Token", newToken);
//         }
//     } catch (err) {
//         return c.json({error: "Invalid token"}, 401);
//     }
// };
