/**
 * Marketing Overview routes — marketing-leads-pipeline.
 * Mounted at /api/marketing/overview (behind authMiddleware in router.ts).
 *
 * GET /    overview page widgets: lead counts, conversion rate, pipeline value,
 *          leads by stage/source, recent activity.
 */
import { Hono } from 'hono'
import type { AppEnv } from '../../types'
import { requirePermission } from '../../middleware/guards'
import { getMarketingOverview } from '@zync/db/queries'

export const overviewRoute = new Hono<AppEnv>()

// GET /api/marketing/overview
overviewRoute.get('/', requirePermission('marketing:read'), async (c) => {
  const session = c.get('session')
  if (!session || session.type !== 'user') return c.json({ error: 'Unauthorized' }, 401)
  const db = c.get('db')

  if (!session.tid) return c.json({ error: 'Unauthorized' }, 401)
  const overview = await getMarketingOverview(db, session.tid)
  return c.json({ overview })
})
