import { APPWRITE_ALMEIRA_CONFIG, projectId, endpoint } from '@/lib/appwrite';
import { createAdminClient, getLoggedInUser } from '@/lib/appwrite.server';
import { Query } from 'node-appwrite';
import { collectionMap, SCHEMA_MAP } from '@/lib/schema';
import { saveEntityAction } from '@/app/actions/entities';
import { redirect } from 'next/navigation';
import type { AuthUser } from '@/types/publications';
/**
* New entity page — uses the specialized PublicationLayout for 'posts',
* and the generic EntityForm for all other entity types.
*/
export default async function NewEntityPage({
params,
}: {
params: Promise<{ entity: string }>;
}) {
const { entity } = await params;
const collectionId = collectionMap[entity];
const fields = SCHEMA_MAP[entity] || [];
if (!collectionId || fields.length === 0) {
return (
Configuración no encontrada para: {entity}
);
}
// Auth check
const user = JSON.parse(JSON.stringify(await getLoggedInUser()));
if (!user) redirect('/login');
// Pre-load all relation data server-side
const relationData: Record> = {};
try {
const client = await createAdminClient();
await Promise.all(
fields
.filter((f) => f.type === 'relation' && f.relationCollection)
.map(async (field) => {
const relKey = field.relationCollection as string;
const relColId = collectionMap[relKey];
if (!relColId) return;
try {
const res = await client.databases.listDocuments(
APPWRITE_ALMEIRA_CONFIG.databaseId,
relColId,
[Query.limit(100)]
);
relationData[field.name] = JSON.parse(JSON.stringify(res.documents));
} catch {
// Silent fail for individual relations
}
})
);
} catch (error) {
console.error('Error loading relation data:', error);
}
const boundSave = saveEntityAction.bind(null, entity);
const appwriteConfig = {
endpoint,
projectId,
databaseId: APPWRITE_ALMEIRA_CONFIG.databaseId,
};
// ── POSTS: Use the new PublicationLayout ──────────────
if (entity === 'posts') {
const { default: PublicationLayout } = await import('@/components/publications/PublicationLayout');
return (
);
}
// ── OTHER ENTITIES: Use the generic EntityForm ────────
const { EntityForm } = await import('@/components/dashboard/EntityForm');
const entityNames: Record = {
categories: 'Categorías', clients: 'Clientes', client_types: 'Tipos de Cliente',
countries: 'Países', departments: 'Departamentos', cities: 'Ciudades',
contracts: 'Contratos', payment_methods: 'Formas de Pago', publication_types: 'Tipos de Publicación',
};
return (
);
}