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

const { UnitBookingOrHoldResponseCodes } = responseCodes;

export const createUnitBookingOrHold = catchAsync(async (req: Request, res: Response) => {
  const { company, id: userId } = req.user;

  const unitBookingOrHold = await unitBookingOrHoldService.createUnitBookingOrHold({
    ...req.body,
    company: company?.id,
    createdBy: userId,
  });

  res.success(unitBookingOrHold, UnitBookingOrHoldResponseCodes.SUCCESS, 'Booking/Hold Created Successfully');
});

export const getUnitBookingOrHoldById = catchAsync(async (req: Request, res: Response) => {
  const { id } = pick(req.params, ['id']);
  const record = await unitBookingOrHoldService.getUnitBookingOrHoldById(id);
  res.success(record, UnitBookingOrHoldResponseCodes.SUCCESS, 'Booking/Hold Fetched Successfully');
});

export const updateUnitBookingOrHold = catchAsync(async (req: Request, res: Response) => {
  const { id } = pick(req.params, ['id']);
  await unitBookingOrHoldService.updateUnitBookingOrHold(id, req.body);
  res.success(true, UnitBookingOrHoldResponseCodes.SUCCESS, 'Booking/Hold Updated Successfully');
});

export const updateBookingDocument = catchAsync(async (req: Request, res: Response) => {
  const { id } = pick(req.params, ['id']);
  const { personIndex, fieldKey, url, additionalDocIndex } = pick(req.body, [
    'personIndex',
    'fieldKey',
    'url',
    'additionalDocIndex',
  ]);
  await unitBookingOrHoldService.updateBookingDocument(id, {
    personIndex,
    fieldKey,
    url,
    additionalDocIndex,
    updatedBy: req.body.updatedBy,
  });
  res.success(
    true,
    UnitBookingOrHoldResponseCodes.SUCCESS,
    'Booking document updated successfully',
  );
});

export const deleteUnitBookingOrHoldById = catchAsync(async (req: Request, res: Response) => {
  const { id } = pick(req.params, ['id']);
  await unitBookingOrHoldService.deleteUnitBookingOrHold(id);
  res.success(null, UnitBookingOrHoldResponseCodes.SUCCESS, 'Booking/Hold Deleted Successfully');
});

export const getUnitBookingOrHolds = catchAsync(async (req: Request, res: Response) => {
  const filter = pick(req.query, ['lead', 'project', 'unit', 'action', 'createdBy', 'updatedBy']);
  const options = pick(req.query, ['sortBy', 'limit', 'page', 'fields', 'populate', 'includeTimeStamps']);

  const result = await unitBookingOrHoldService.queryUnitBookingOrHold(filter, options);
  res.success(result, UnitBookingOrHoldResponseCodes.SUCCESS, 'Booking/Hold Records Fetched Successfully');
});

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

    const record = await unitBookingOrHoldService.getLatestHoldByUnitId(unitId);

    res.success(
      record,
      UnitBookingOrHoldResponseCodes.SUCCESS,
      'Hold Record Fetched Successfully',
    );
  },
);
