import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "@lila/db";
import * as schema from "@lila/db/schema";

export const auth = betterAuth({
  baseURL: process.env["BETTER_AUTH_URL"] || "http://localhost:3000",
  advanced: {
    cookiePrefix: "lila",
    defaultCookieAttributes: {
      ...(process.env["COOKIE_DOMAIN"] && {
        domain: process.env["COOKIE_DOMAIN"],
      }),
      secure: !!process.env["COOKIE_DOMAIN"],
      sameSite: "lax" as const,
    },
  },
  database: drizzleAdapter(db, { provider: "pg", schema }),
  trustedOrigins: [process.env["CORS_ORIGIN"] || "http://localhost:5173"],
  socialProviders: {
    google: {
      clientId: process.env["GOOGLE_CLIENT_ID"] as string,
      clientSecret: process.env["GOOGLE_CLIENT_SECRET"] as string,
    },
    github: {
      clientId: process.env["GITHUB_CLIENT_ID"] as string,
      clientSecret: process.env["GITHUB_CLIENT_SECRET"] as string,
    },
  },
});
