import { PrismaAdapter } from '@lucia-auth/adapter-prisma'
import { Lucia, TimeSpan } from 'lucia'
import prisma from '../prisma/client.ts'

declare module 'lucia' {
  interface Register {
    Lucia: typeof auth
    UserId: string
    DatabaseUserAttributes: DatabaseUserAttributes
    DatabaseSessionAttributes: DatabaseSessionAttributes
  }
}

interface DatabaseUserAttributes {
  id: string
  displayName: string
  username: string
}

interface DatabaseSessionAttributes {
  livekitToken: string | null
}

export const SESSION_EXPIRES_IN = new TimeSpan(30, 'd')

export const auth = new Lucia(new PrismaAdapter(prisma.session, prisma.user), {
  sessionExpiresIn: SESSION_EXPIRES_IN,
  sessionCookie: {
    attributes: {
      sameSite: 'none',
    },
  },
  getUserAttributes: ({ id, displayName, username }) => {
    return {
      id,
      displayName,
      username,
    }
  },
  getSessionAttributes: ({ livekitToken }) => {
    return {
      livekitToken,
    }
  },
})

export type Auth = typeof auth
