import NextAuth from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials"

export const { handlers, auth, signIn, signOut } = NextAuth({
  providers: [
    CredentialsProvider({
      name: "Credentials",
      credentials: {
        email: { label: "Email", type: "email" },
        password: { label: "Password", type: "password" }
      },
      async authorize(credentials) {
        // Boilerplate mock logic
        // TODO: In production, lookup user in Prisma and verify password using bcrypt
        // const user = await db.user.findUnique({ where: { email: credentials.email } })
        
        if (credentials?.email === "admin@ayris.tech" && credentials?.password === "admin") {
          return {
            id: "1",
            name: "Admin User",
            email: "admin@ayris.tech",
            role: "ADMIN"
          }
        }
        
        return null
      }
    })
  ],
  callbacks: {
    async jwt({ token, user }) {
      if (user) {
        token.role = (user as any).role
      }
      return token
    },
    async session({ session, token }) {
      if (session.user && token.role) {
        (session.user as any).role = token.role
      }
      return session
    }
  },
  pages: {
    signIn: '/login'
  }
})
