import { Request, Response } from 'express';

import * as whatsappService from '@/modules/communication/whatsapp/whatsapp.service';
import catchAsync from '@/shared/utils/catchAsync';
import { pick } from '@/shared/utils';
import responseCodes from '@/shared/utils/responseCode/responseCode';

const { WhatsappResponseCodes } = responseCodes;

export const getWhatsapps = catchAsync(async (req: Request, res: Response) => {
  console.log('🚀 ~ req:', req.subdomains);
  const companyId = req.user.company.id;
  const filter = pick(req.query, ['search']);
  const options = pick(req.query, [
    'sortBy',
    'limit',
    'page',
    'populate',
    'includeTimeStamps',
  ]);

  const whatsapps = await whatsappService.queryWhatsapps(
    filter,
    options,
    companyId,
  );

  res.success(
    whatsapps,
    WhatsappResponseCodes.SUCCESS,
    'Whatsapp Fetched Successfully',
  );
});

export const updateTemplate = catchAsync(
  async (req: Request, res: Response) => {
    const { templateId } = pick(req.params, ['templateId']);

    const result = await whatsappService.updateTemplate(templateId, req.body);
    res.success(
      result,
      WhatsappResponseCodes.SUCCESS,
      'Template Updated Successfully',
    );
  },
);

export const addWhatsappCreds = catchAsync(
  async (req: Request, res: Response) => {
    const companyId = req.user.company.id;
    const result = await whatsappService.addWhatsappCreds(companyId, req.body);
    res.success(
      result,
      WhatsappResponseCodes.SUCCESS,
      'Whatsapp Credentials Added Successfully',
    );
  },
);
