import NextAuth from "next-auth"
import type { NextAuthConfig } from "next-auth"
import Credentials from "next-auth/providers/credentials"

const config = {
    session: { strategy: "jwt" , maxAge: 86400 },
    pages: {
        signIn: "/login",
    },
    debug: false,
    providers: [
        Credentials({
            id: 'credentials',
            name: 'credentials',
            credentials: {
                email: { label: "email", type: "email" },
                password: { label: "Password", type: "password" },
            },
            async authorize(credentials) {
                const { email, password } = credentials ?? {};
            
                if (!email || !password) {
                    return null; // Will trigger the "CredentialsSignin" error
                }
            
                if (email === "admin@balimanagement.villas" && password === "Tr7f0dmoMY74X4p0D9") {
                    return {
                        id: "1",
                        name: "Admin Dong",
                        email: "admin@balimanagement.villas",
                        image: "",
                    };
                }

                return null
            }            
        }),
    ],
    callbacks: {
        authorized({request, auth}){
            const { pathname } = request.nextUrl
            if( pathname.startsWith('/')) return !!auth
            return true
        },
    },
} satisfies NextAuthConfig

export const {
    handlers,
    auth,
    signIn,
    signOut,
} = NextAuth(config)