import { createDirectus, rest, authentication } from '@directus/sdk';
import * as dotenv from 'dotenv';

dotenv.config();

/**
 * Get authenticated Directus client
 */
export async function getAuthenticatedClient() {
  const client = createDirectus(process.env.DIRECTUS_URL || 'http://localhost:8055')
    .with(authentication())
    .with(rest());

  // Authenticate if credentials are provided
  if (process.env.DIRECTUS_EMAIL && process.env.DIRECTUS_PASSWORD) {
    await client.login(process.env.DIRECTUS_EMAIL, process.env.DIRECTUS_PASSWORD);
  }

  return client;
}