import "server-only"

import type { Session } from "next-auth"

export function isValidSession(session: Session | null): session is Session {
  if (!session) {
    return false
  }
  if (session.error) {
    console.log(`Session error: ${session.error}`)
    return false
  }

  const token = session.token

  if (token?.error) {
    console.log(`Session token error: ${token.error}`)
    return false
  }
  if (token?.expires_at && token.expires_at < Date.now()) {
    console.log(`Session expired: ${session.token.expires_at}`)
    return false
  }

  return true
}
