import { readPocketBaseSchema, fieldsOf } from './pb-schema.js';

const { absPath, collections, byId } = readPocketBaseSchema();

const businessCollections = collections.filter(c => !c.name.startsWith('_'));
const authCollections = collections.filter(c => c.type === 'auth');
const relationCount = collections.reduce(
  (count, collection) => count + fieldsOf(collection).filter(field => field.type === 'relation').length,
  0,
);

console.log(`PocketBase schema: ${absPath}`);
console.log(`Collections: ${collections.length}`);
console.log(`Business collections: ${businessCollections.length}`);
console.log(`Auth collections: ${authCollections.map(c => c.name).join(', ') || 'none'}`);
console.log(`Relation fields: ${relationCount}`);
console.log('');

for (const collection of businessCollections.sort((a, b) => a.name.localeCompare(b.name))) {
  const fields = fieldsOf(collection);
  const relationFields = fields.filter(field => field.type === 'relation');
  const relationSummary = relationFields
    .map(field => {
      const target = field.collectionId ? byId.get(field.collectionId)?.name || field.collectionId : '?';
      return `${field.name}->${target}`;
    })
    .join(', ');

  console.log(`${collection.name.padEnd(24)} ${collection.type.padEnd(6)} fields=${String(fields.length).padStart(2)} relations=${relationSummary || '-'}`);
}
