import { Request, Response } from 'express';

import * as companyService from '@/modules/company/company.service';
import catchAsync from '@/shared/utils/catchAsync';
import responseCodes from '@/shared/utils/responseCode/responseCode';
import { pick } from '@/shared/utils';
import { Status } from '@/shared/constants/enum.constant';

const { CompanyResponseCodes } = responseCodes;

export const createCompany = catchAsync(async (req: Request, res: Response) => {
  const company = await companyService.createCompany(req.body);
  res.success(
    company,
    CompanyResponseCodes.SUCCESS,
    'Company Created Successfully',
  );
});

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

  const updatedCompany = await companyService.updateCompany(id, req.body);
  res.success(
    updatedCompany,
    CompanyResponseCodes.SUCCESS,
    'Company Updated Successfully',
  );
});

export const tempActivateCompany = catchAsync(
  async (_req: Request, res: Response) => {
    const companyId = '68df731d065d291eecb155ae';

    const updatedCompany = await companyService.updateCompany(companyId, {
      status: Status.ACTIVE,
    });

    res.success(
      updatedCompany,
      CompanyResponseCodes.SUCCESS,
      'Company Activated Successfully',
    );
  },
);

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

    const company = await companyService.deleteCompany(id);
    res.success(
      company,
      CompanyResponseCodes.SUCCESS,
      'Company Deleted Successfully',
    );
  },
);

export const getCompanyById = catchAsync(
  async (req: Request, res: Response) => {
    const { id } = pick(req.params, ['id']);
    const { fields, populate } = pick(req.query, ['fields', 'populate']);

    const companies = await companyService.getCompanyById(id, fields, populate);

    res.success(
      companies,
      CompanyResponseCodes.SUCCESS,
      'Company Fetched Successfully',
    );
  },
);

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

  const companies = await companyService.queryCompanies(filter, options);

  res.success(
    companies,
    CompanyResponseCodes.SUCCESS,
    'Company Fetched Successfully',
  );
});
