import { Request, Response } from 'express';

import * as documentsService from '@/modules/documents/documents.service';
import catchAsync from '@/shared/utils/catchAsync';
import responseCodes from '@/shared/utils/responseCode/responseCode';
import { pick } from '@/shared/utils';

const { DocumentsResponseCodes } = responseCodes;

export const createDocuments = catchAsync(async (req: Request, res: Response) => {
  const documents = await documentsService.createDocuments(req.body);
  res.success(documents, DocumentsResponseCodes.SUCCESS, 'Documents Created Successfully');
});

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

  const updatedDocuments = await documentsService.updateDocuments(id, req.body);
  res.success(
    updatedDocuments,
    DocumentsResponseCodes.SUCCESS,
    'Documents Updated Successfully',
  );
});

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

    const documents = await documentsService.deleteDocuments(id);
    res.success(documents, DocumentsResponseCodes.SUCCESS, 'Documents Deleted Successfully');
  },
);

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

  const documentss = await documentsService.getDocumentsById(id);

  res.success(
    documentss,
    DocumentsResponseCodes.SUCCESS,
    'Documents Fetched Successfully',
  );
});

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

  const documentss = await documentsService.queryDocuments(filter, options);

  res.success(
    documentss,
    DocumentsResponseCodes.SUCCESS,
    'Documents Fetched Successfully',
  );
});
