'use client';
import { useEffect, useState } from 'react';
import { functions } from '@/lib/appwrite';

export default function CajuFunctionPage() {
  const [output, setOutput] = useState<string>('Executando…');

  useEffect(() => {
    (async () => {
      try {
        const exec = await functions.createExecution(
          process.env.NEXT_PUBLIC_APPWRITE_FUNCTION_CAJU_ID as string,
          '',
          false
        );
        setOutput(exec.responseBody || '⚠️ Sem resposta');
      } catch (err: any) {
        setOutput(err?.message ?? 'Erro desconhecido');
      }
    })();
  }, []);

  return (
    <main className="p-6 max-w-3xl mx-auto">
      <h1 className="text-2xl font-semibold mb-4">Função Caju</h1>
      <pre className="bg-gray-800 text-green-400 rounded p-4 whitespace-pre-wrap break-words">
        {output}
      </pre>
    </main>
  );
}
