import { Request, Response } from 'express';

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

const { Fast2smsResponseCodes } = responseCodes;

export const getFast2smss = 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 fast2smss = await fast2smsService.queryFast2smss(
    filter,
    options,
    companyId,
  );

  res.success(
    fast2smss,
    Fast2smsResponseCodes.SUCCESS,
    'Fast2sms Fetched Successfully',
  );
});

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

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

export const addFast2smsCreds = catchAsync(
  async (req: Request, res: Response) => {
    const companyId = req.user.company.id;
    const result = await fast2smsService.addFast2smsCreds(companyId, req.body);
    res.success(
      result,
      Fast2smsResponseCodes.SUCCESS,
      'Fast2sms Credentials Added Successfully',
    );
  },
);

export const bulkUploadTemplate = catchAsync(
  async (req: Request, res: Response) => {
    const companyId = req.user.company.id;
    const result = await fast2smsService.bulkUpsertFast2smsTemplates(
      companyId,
      req.body,
    );
    res.success(
      result,
      Fast2smsResponseCodes.SUCCESS,
      'Fast2sms Template Added Successfully',
    );
  },
);
