import { NextRequest, NextResponse } from 'next/server';
import { db, plans } from '@server/db/drizzle';
import { eq, asc } from 'drizzle-orm';
import { wrapRouteHandler } from '@server/logger/request';

async function handleGET(_req: NextRequest) {
  try {
    const rows = await db.select({
      id: plans.id,
      name: plans.name,
      price_monthly: plans.priceMonthly,
      price_annual: plans.priceAnnual,
      features: plans.features,
      limits: plans.limits,
      is_active: plans.isActive,
    })
    .from(plans)
    .where(eq(plans.isActive, true))
    .orderBy(asc(plans.priceMonthly));

    return NextResponse.json({ plans: rows }, {
      headers: { 'Cache-Control': 'public, max-age=300, stale-while-revalidate=600' },
    });
  } catch {
    return NextResponse.json({ plans: [] });
  }
}

export const GET = wrapRouteHandler((req: Request) => handleGET(req as NextRequest));
