import { getIronSession, IronSessionOptions } from "iron-session";
import { FastifyRequest, FastifyReply } from "fastify";

export interface SessionData {
  user?: {
    id: string;
    email: string;
    name: string;
  };
  accessToken?: string;
  idToken?: string;
}

const sessionOptions: IronSessionOptions = {
  password: process.env.SESSION_SECRET!,
  cookieName: "tina_session",
  cookieOptions: {
    secure: process.env.NODE_ENV === "production",
    httpOnly: true,
    sameSite: "lax",
    maxAge: 60 * 60 * 24 * 7, // 7 jours
  },
};

export async function getSession(req: FastifyRequest, res: FastifyReply) {
  return getIronSession<SessionData>(req.raw, res.raw, sessionOptions);
}
