import { createCookieSessionStorage } from 'react-router'
import { type User } from './auth'

type SessionData = {
  user: User
  returnTo: string
}

type SessionFlashData = {
  error: string
  success: string
  info: string
}

export function createSessionStorage(env: any) {
  const cookieSessionStorage = createCookieSessionStorage<
    SessionData,
    SessionFlashData
  >({
    cookie: {
      name: '__session',
      httpOnly: true,
      secure: false,
      secrets: [env.SESSION_SECRET],
      sameSite: 'lax',
      path: '/',
      maxAge: 86400 * 7 // 7 days
    }
  })
  return cookieSessionStorage
}
