// src/app/lib/appwrite.js
import { Client, Account, ID } from "appwrite";

// Validate environment variables at build time
const endpoint = process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT;
const projectId = process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID;

if (!endpoint || !projectId) {
  throw new Error(`
    Appwrite configuration error:
    NEXT_PUBLIC_APPWRITE_ENDPOINT = ${endpoint}
    NEXT_PUBLIC_APPWRITE_PROJECT_ID = ${projectId}
    Please check your environment variables
  `);
}

// Initialize client
const client = new Client()
  .setEndpoint(endpoint)
  .setProject(projectId);

const account = new Account(client);

// Debug logs (remove in production)
if (process.env.NODE_ENV !== 'production') {
  console.log('Appwrite initialized with:');
  console.log('Endpoint:', endpoint);
  console.log('Project ID:', projectId);
}

export { client, account, ID };