import { Request, Response } from 'express';

import * as fileService from '@/modules/project/files/files.service';
import catchAsync from '@/shared/utils/catchAsync';
import responseCodes from '@/shared/utils/responseCode/responseCode';
import { pick } from '@/shared/utils';

const { FileResponseCodes } = responseCodes;

export const createFile = catchAsync(async (req: Request, res: Response) => {
  const result = await fileService.createFile(req.body);
  res.success(result, FileResponseCodes.SUCCESS, 'File Created Successfully');
});

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

  const result = await fileService.updateFile(id, req.body);
  res.success(result, FileResponseCodes.SUCCESS, 'File Updated Successfully');
});

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

  const result = await fileService.deleteFile(id);
  res.success(result, FileResponseCodes.SUCCESS, 'File Deleted Successfully');
});

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

  const result = await fileService.getFileById(id);
  res.success(result, FileResponseCodes.SUCCESS, 'File Fetched Successfully');
});

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

  const result = await fileService.queryFiles(filter, options);
  res.success(result, FileResponseCodes.SUCCESS, 'Files Fetched Successfully');
});