import type { PageServerLoad } from './$types';
import { getContent } from '$lib/server/content';
import { listSubmissions } from '$lib/server/submissions';
import { DEFAULT_SLOTS } from '$lib/content/defaults';
import { aggregateScore, analyzeCopy } from '$lib/seo';

export const load: PageServerLoad = () => {
  const content = getContent();
  const subs = listSubmissions();

  const analyses = DEFAULT_SLOTS
    .filter((s) => s.kind === 'multiline' || (s.id.endsWith('_text') && s.kind !== 'inline'))
    .slice(0, 30)
    .map((s) => analyzeCopy(content.copy[s.id] ?? s.value, content.seo.primaryKeywords));

  const scores = aggregateScore(analyses);

  return {
    totalSlots: DEFAULT_SLOTS.length,
    totalImages: Object.keys(content.images).length,
    totalSubmissions: subs.length,
    recentSubmissions: subs.slice(0, 5),
    scores,
    seo: content.seo
  };
};
