import { Request, Response } from 'express';

import * as dashboardService from '@/modules/dashboard/dashboard.service';
import catchAsync from '@/shared/utils/catchAsync';
import responseCodes from '@/shared/utils/responseCode/responseCode';
import { pick } from '@/shared/utils';
import { getObjectId } from '@/shared/utils/commonHelper';

const { DashboardResponseCodes } = responseCodes;

export const getBrokerDashboard = catchAsync(
  async (req: Request, res: Response) => {
    const { id, userType, company } = req.user;

    const filters = pick(req.query, ['projectId', 'startDate', 'endDate']);

    const dashboardData = await dashboardService.getBrokerDashboard(
      id,
      userType,
      company.id,
      filters,
    );

    res.success(
      dashboardData,
      DashboardResponseCodes.SUCCESS,
      'Broker Dashboard fetched successfully',
    );
  },
);

export const getTeamActivitySummaryReport = catchAsync(
  async (req: Request, res: Response) => {
    const { id, userType, company } = req.user;

    const filters = pick(req.query, ['startDate', 'endDate', 'source']);

    const dashboardData = await dashboardService.getTeamActivitySummaryReport(
      id,
      userType,
      company.id,
      filters,
    );

    res.success(
      dashboardData,
      DashboardResponseCodes.SUCCESS,
      'Team Activity Summary Report fetched successfully',
    );
  },
);

export const getBuilderDashboard = catchAsync(
  async (req: Request, res: Response) => {
    const { id, userType, company } = req.user;

    const dashboardData = await dashboardService.getBuilderDashboard(
      getObjectId(id),
      userType,
      getObjectId(company.id),
    );

    res.success(
      dashboardData,
      DashboardResponseCodes.SUCCESS,
      'Builder Dashboard fetched successfully',
    );
  },
);

export const getSuperAdminDashboard = catchAsync(
  async (req: Request, res: Response) => {
    const { id, company } = req.user;
    const filter = pick(req.query, ['startDate', 'endDate']);

    const dashboardData = await dashboardService.getSuperAdminDashboard(
      getObjectId(id),
      getObjectId(company.id),
      filter,
    );

    res.success(
      dashboardData,
      DashboardResponseCodes.SUCCESS,
      'SuperAdmin Dashboard fetched successfully',
    );
  },
);
