// lib/api.js
import { databases, Query } from "./appwrite";
import { DATABASE_ID, COLLECTION_ID } from "./config";

export async function getTodayTokens() {
  const today = new Date().toISOString().split('T')[0];
  return databases.listDocuments(
    DATABASE_ID,
    COLLECTION_ID,
    [
      Query.equal('date', today),
      Query.orderAsc('tokenId')
    ]
  );
}

export async function getPatientTokens(patientId, date) {
  return databases.listDocuments(
    DATABASE_ID,
    COLLECTION_ID,
    [
      Query.equal('patientId', patientId),
      Query.equal('date', date),
      Query.notEqual('status', 'done')
    ]
  );
}

export async function createToken(document) {
  return databases.createDocument(
    DATABASE_ID,
    COLLECTION_ID,
    ID.unique(),
    document
  );
}

export async function updateTokenStatus(documentId, status) {
  return databases.updateDocument(
    DATABASE_ID,
    COLLECTION_ID,
    documentId,
    { status }
  );
}

export async function getSettings() {
  return databases.getDocument(
    DATABASE_ID,
    'settings',
    'current_settings' // Single document ID
  );
}

export { DATABASE_ID, COLLECTION_ID };