import session from 'express-session';
import connectPgSimple from 'connect-pg-simple';
import { pool } from './db';

const PgSession = connectPgSimple(session);

export const sessionConfig = {
  store: new PgSession({
    pool: pool,
    tableName: 'sessions',
    createTableIfMissing: false, // We handle this with Drizzle
  }),
  secret: process.env.SESSION_SECRET || 'dev-session-secret-mediflash-sos-2024',
  resave: false,
  saveUninitialized: false,
  cookie: {
    secure: process.env.NODE_ENV === 'production' && process.env.REPL_DEPLOYMENT !== 'true',
    httpOnly: true,
    maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days
    sameSite: process.env.NODE_ENV === 'production' ? 'strict' as const : 'lax' as const,
  },
  name: 'mediflash.sid', // Custom session name
};

export const sessionMiddleware = session(sessionConfig);