import { betterAuth } from 'better-auth'
import { genericOAuth } from 'better-auth/plugins'
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
import * as authSchema from '@/db/auth-schema'
import db from '@/db'

export const auth = betterAuth({
    database: drizzleAdapter(db, {
        provider: 'sqlite',
        schema: authSchema,
    }),
    plugins: [
        genericOAuth({
            config: [
                {
                    providerId: 'unipi',
                    clientId: process.env.OAUTH_CLIENT_ID!,
                    clientSecret: process.env.OAUTH_CLIENT_SECRET!,
                    scopes: process.env.OAUTH_SCOPES!.split(' '),
                    userInfoUrl: process.env.OAUTH_USER_INFO_URL!,
                    authorizationUrl: process.env.OAUTH_AUTH_URL!,
                    tokenUrl: `${process.env.OAUTH_TOKEN_HOST}${process.env.OAUTH_TOKEN_PATH}`,
                    redirectURI: process.env.OAUTH_REDIRECT_URL!,
                },
            ],
        }),
    ],
})
