import { Request, Response } from 'express';

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

const { PropertyFileResponseCodes } = responseCodes;

export const createPropertyFile = catchAsync(
  async (req: Request, res: Response) => {
    const result = await propertyFileService.createPropertyFile(req.body);
    res.success(
      result,
      PropertyFileResponseCodes.SUCCESS,
      'Property File Created Successfully',
    );
  },
);

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

    const result = await propertyFileService.updatePropertyFile(id, req.body);
    res.success(
      result,
      PropertyFileResponseCodes.SUCCESS,
      'Property File Updated Successfully',
    );
  },
);

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

    const result = await propertyFileService.deletePropertyFile(id);
    res.success(
      result,
      PropertyFileResponseCodes.SUCCESS,
      'Property File Deleted Successfully',
    );
  },
);

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

    const result = await propertyFileService.getPropertyFileById(id);
    res.success(
      result,
      PropertyFileResponseCodes.SUCCESS,
      'Property File Fetched Successfully',
    );
  },
);

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

    const result = await propertyFileService.queryPropertyFiles(
      filter,
      options,
    );
    res.success(
      result,
      PropertyFileResponseCodes.SUCCESS,
      'Property Files Fetched Successfully',
    );
  },
);
