import { db } from '@/lib/prisma';
import { parsePlanRules } from '@/modules/plan/services/plan-rules.service';
import type { AppContext } from '@/types/hono';

export async function listPlansHandler(c: AppContext) {
  const plans = await db.plan.findMany({
    orderBy: {
      price: 'asc',
    },
  });

  return c.json(
    plans.map((plan) => {
      const rules = parsePlanRules(plan.rules);
      return {
        code: plan.code,
        name: plan.name,
        price: Number(plan.price),
        features: rules.features,
        limits: rules.limits,
      };
    })
  );
}
