import { betterAuth } from "better-auth";
import { genericOAuth } from "better-auth/plugins";
import { Database } from "bun:sqlite";

export const auth = betterAuth({
  appName: "Streamarr",
  basePath: "/api/auth",
  // database: new Database('auth.sqlite', { create: true }),
  database: new Database(':memory:'),
  logger: {
    level: "debug",
    log(level, message, ...args) {
      console.log(level, message, {args});
    },
  },
  user: {
    additionalFields: {
      name: {
        type: "string",
        nullable: true,
      },
      username: {
        type: "string",
        nullable: true,
      },
    },
  },
  plugins: [
    genericOAuth({
      config: [
        {
          providerId: "zitadel",
          clientId: "",
          clientSecret: "",
          discoveryUrl: "https://auth.amarth.cloud/.well-known/openid-configuration",
          scopes: [
            "offline_access",
            "openid",
            "email",
            "picture",
            "profile",
            "groups",
          ],
          accessType: "offline",
          pkce: true,
          mapProfileToUser: ({ id, name, email, image, preferred_username, emailVerified }) => 
            ({ id, name, email, emailVerified, image, username: preferred_username }),
        },
      ],
    }),
  ],
});
