import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { getCalculatorBySlug, getCalculatorsByCategory, categories } from '$lib/data/calculators';

export const load: PageServerLoad = ({ params }) => {
  const calc = getCalculatorBySlug(params.slug);

  if (!calc) {
    throw error(404, {
      message: `Calculator "${params.slug}" not found`
    });
  }

  // Get related calculators from the same category (excluding this one)
  const related = getCalculatorsByCategory(calc.category)
    .filter(c => c.slug !== calc.slug)
    .slice(0, 8);

  const categoryMeta = categories[calc.category];

  return {
    calculator: calc,
    related,
    categoryLabel: categoryMeta?.label ?? calc.category,
    categoryIcon: categoryMeta?.icon ?? '🔢'
  };
};
