import { Request, Response } from 'express';
import * as channelPartnerService from './channelPartner.service';
import catchAsync from '@/shared/utils/catchAsync';
import responseCodes from '@/shared/utils/responseCode/responseCode';
import { pick } from '@/shared/utils';

const { ChannelPartnerResponseCodes } = responseCodes;

// ===================== CP Company =====================

export const createCPCompany = catchAsync(
  async (req: Request, res: Response) => {
    const cpCompany = await channelPartnerService.createCPCompany(req.body);
    res.success(
      cpCompany,
      ChannelPartnerResponseCodes.SUCCESS,
      'Channel partner company created successfully',
    );
  },
);

export const getCPCompanies = catchAsync(
  async (req: Request, res: Response) => {
    const filter = pick(req.query, ['search', 'status', 'companyId']);
    const options = pick(req.query, [
      'sortBy',
      'limit',
      'page',
      'populate',
      'fields',
      'includeTimeStamps',
    ]);

    const result = await channelPartnerService.queryCPCompanies(
      filter,
      options,
    );
    res.success(
      result,
      ChannelPartnerResponseCodes.SUCCESS,
      'Channel partner companies fetched successfully',
    );
  },
);

export const getCPCompanyById = catchAsync(
  async (req: Request, res: Response) => {
    const { id } = pick(req.params, ['id']);
    const cpCompany = await channelPartnerService.getCPCompanyById(id);
    res.success(
      cpCompany,
      ChannelPartnerResponseCodes.SUCCESS,
      'Channel partner company fetched successfully',
    );
  },
);

export const updateCPCompany = catchAsync(
  async (req: Request, res: Response) => {
    const { id } = pick(req.params, ['id']);
    const cpCompany = await channelPartnerService.updateCPCompany(id, req.body);
    res.success(
      cpCompany,
      ChannelPartnerResponseCodes.SUCCESS,
      'Channel partner company updated successfully',
    );
  },
);

export const deleteCPCompany = catchAsync(
  async (req: Request, res: Response) => {
    const { id } = pick(req.params, ['id']);
    await channelPartnerService.deleteCPCompany(id);
    res.success(
      true,
      ChannelPartnerResponseCodes.SUCCESS,
      'Channel partner company deleted successfully',
    );
  },
);

export const bulkUpdateCPCompanyStatus = catchAsync(
  async (req: Request, res: Response) => {
    const { ids, status } = req.body;
    const count = await channelPartnerService.bulkUpdateCPCompanyStatus(
      ids,
      status,
    );
    res.success(
      { modifiedCount: count },
      ChannelPartnerResponseCodes.SUCCESS,
      `${count} channel partner(s) status updated successfully`,
    );
  },
);

export const bulkDeleteCPCompanies = catchAsync(
  async (req: Request, res: Response) => {
    const { ids } = req.body;
    const count = await channelPartnerService.bulkDeleteCPCompanies(ids);
    res.success(
      { deletedCount: count },
      ChannelPartnerResponseCodes.SUCCESS,
      `${count} channel partner(s) deleted successfully`,
    );
  },
);

// ===================== CP Contact =====================

export const createCPContact = catchAsync(
  async (req: Request, res: Response) => {
    const { cpCompany, contacts } = req.body;
    const companyId = req.user?.company?.id || req.body.company;
    const createdBy = req.user?.id || req.body.createdBy;

    const result = await channelPartnerService.createCPContacts(
      cpCompany,
      contacts,
      companyId,
      createdBy,
    );
    res.success(
      result,
      ChannelPartnerResponseCodes.SUCCESS,
      'Channel partner contact(s) created successfully',
    );
  },
);

export const getCPContacts = catchAsync(async (req: Request, res: Response) => {
  const filter = pick(req.query, [
    'cpCompany',
    'search',
    'status',
    'companyId',
  ]);
  const options = pick(req.query, [
    'sortBy',
    'limit',
    'page',
    'populate',
    'fields',
    'includeTimeStamps',
  ]);

  const result = await channelPartnerService.queryCPContacts(filter, options);
  res.success(
    result,
    ChannelPartnerResponseCodes.SUCCESS,
    'Channel partner contacts fetched successfully',
  );
});

export const updateCPContact = catchAsync(
  async (req: Request, res: Response) => {
    const { id } = pick(req.params, ['id']);
    const contact = await channelPartnerService.updateCPContact(id, req.body);
    res.success(
      contact,
      ChannelPartnerResponseCodes.SUCCESS,
      'Channel partner contact updated successfully',
    );
  },
);

export const deleteCPContact = catchAsync(
  async (req: Request, res: Response) => {
    const { id } = pick(req.params, ['id']);
    await channelPartnerService.deleteCPContact(id);
    res.success(
      true,
      ChannelPartnerResponseCodes.SUCCESS,
      'Channel partner contact deleted successfully',
    );
  },
);

// ===================== CP Sales =====================

export const getCPSales = catchAsync(async (req: Request, res: Response) => {
  const filter = pick(req.query, ['cpCompany', 'companyId', 'search']);
  const options = pick(req.query, [
    'sortBy',
    'limit',
    'page',
    'includeTimeStamps',
  ]);

  const result = await channelPartnerService.queryCPSales(filter, options);
  res.success(
    result,
    ChannelPartnerResponseCodes.SUCCESS,
    'CP sales fetched successfully',
  );
});

// ===================== CP Payment =====================

export const recordPayment = catchAsync(async (req: Request, res: Response) => {
  const payment = await channelPartnerService.recordPayment(req.body);
  res.success(
    payment,
    ChannelPartnerResponseCodes.SUCCESS,
    'Payment recorded successfully',
  );
});

export const getPayments = catchAsync(async (req: Request, res: Response) => {
  const filter = pick(req.query, ['cpCompany', 'companyId']);
  const options = pick(req.query, ['sortBy', 'limit', 'page']);

  const result = await channelPartnerService.queryPayments(filter, options);
  res.success(
    result,
    ChannelPartnerResponseCodes.SUCCESS,
    'Payments fetched successfully',
  );
});
