import { ac, admin, user } from '@lib/auth/permissions';
import { adminClient, organizationClient } from 'better-auth/client/plugins';
import { createAuthClient } from 'better-auth/react';
import {
  acOrg,
  adminOrg,
  member,
  owner,
} from './auth/organization-permissions';

export const authClient = createAuthClient({
  baseURL: process.env.BETTER_AUTH_URL || 'http://localhost:3000',
  plugins: [
    adminClient({
      ac,
      roles: { admin, user },
      defaultRole: 'user',
    }),
    organizationClient({
      ac: acOrg,
      roles: { owner, admin: adminOrg, member },
      schema: {
        organization: {
          modelName: 'house',
          additionalFields: {
            color: {
              type: 'string',
              defaultValue: '#000000',
            },
          },
        },
      },
    }),
  ],
});

export const { useSession, getSession } = authClient;

export type ClientSession = typeof authClient.$Infer.Session;
