import { Request, Response, NextFunction } from 'express';
import { getOverview, getPlatformTimeseries } from './analytics.service';

export async function getCaptureLeadsOverview(req: Request, res: Response, next: NextFunction) {
  try {
    const { from, to } = req.query as { from: string; to: string };
    const userCompany = (req as unknown as { user?: { company?: { id?: string } | string } }).user?.company;
    const companyId = typeof userCompany === 'string' ? userCompany : userCompany?.id;
    const data = await getOverview(companyId, from, to);
    res.success(data, 200, 'Analytics Overview Fetched');
  } catch (err) {
    next(err);
  }
}

export async function getCaptureLeadsPlatformTimeseries(req: Request, res: Response, next: NextFunction) {
  try {
    const { from, to } = req.query as { from: string; to: string };
    const userCompany = (req as unknown as { user?: { company?: { id?: string } | string } }).user?.company;
    const companyId = typeof userCompany === 'string' ? userCompany : userCompany?.id;
    const data = await getPlatformTimeseries(companyId, from, to);
    res.success(data, 200, 'Analytics Timeseries Fetched');
  } catch (err) {
    next(err);
  }
}
