import { Request, Response } from 'express';

import catchAsync from '@/shared/utils/catchAsync';
// import { sendEmailNotification } from '@/producers/notification/notification.producer';
// import { EmailType } from '@/shared/constants/enum.constant';
import responseCodes from '@/shared/utils/responseCode/responseCode';
import { pick } from '@/shared/utils';

import * as userService from '@/modules/user/user.service';
import { PaginateOptions } from '@/shared/utils/plugins/paginate/paginate';

const { UserResponseCodes } = responseCodes;

export const createUser = catchAsync(async (req: Request, res: Response) => {
  const user = await userService.createUser(req.body);
  // sendEmailNotification({
  //   to: req.body.email,
  //   subject: 'Welcome to our app',
  //   body: 'Welcome to our app',
  //   type: EmailType?.[0],
  // });

  const message = user.emailSent
    ? 'User Created Successfully. Set password email sent.'
    : 'User Created Successfully. Failed to send set password email.';

  res.success(user, UserResponseCodes.SUCCESS, message);
});

export const getUsers = catchAsync(async (req: Request, res: Response) => {
  const filter = pick(req.query, [
    'name',
    'role',
    'company',
    'status',
    'search',
    'team',
    'companyType',
    'isTeamLead',
    'teamLeadId',
    'userType',
    'totalActiveUsers',
  ]);
  const options: PaginateOptions = pick(req.query, [
    'sortBy',
    'limit',
    'page',
    'projectBy',
    'populate',
    'fields',
    'alias',
    'includeTimeStamps',
  ]);
  const result = await userService.queryUsers(filter, options);
  // sendEmailNotification({
  //   to: 'Test@gmail.com',
  //   subject: 'Welcome to our app',
  //   body: 'Welcome to our app',
  //   type: EmailType?.[0],
  // });
  res.success(result, UserResponseCodes.SUCCESS, 'Users Fetched Successfully');
});

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

  const user = await userService.getUserById(userId, fields, populate);

  res.success(user, UserResponseCodes.SUCCESS, 'User Fetched Successfully');
});

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

  const user = await userService.updateUserById(userId, req.body);
  res.success(user, UserResponseCodes.SUCCESS, 'User Updated Successfully');
});

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

  const user = await userService.deleteUserById(userId);
  res.success(user, UserResponseCodes.SUCCESS, 'User Deleted Successfully');
});

export const resetPassword = catchAsync(async (req: Request, res: Response) => {
  const { userId } = pick(req.params, ['userId']);
  const { newPassword, oldPassword } = req.body;

  const user = await userService.resetPassword(
    userId,
    newPassword,
    oldPassword,
  );
  res.success(user, UserResponseCodes.SUCCESS, 'Password Reset Successfully');
});

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

    const result = await userService.getSidebarAnalytics({
      userId,
      company: company.id,
      userType,
    });
    res.success(
      result,
      UserResponseCodes.SUCCESS,
      'Sidebar Analytics Fetched Successfully',
    );
  },
);
