import { Request, Response } from "express";
import AppError from "../errors/AppError";
import * as Yup from "yup";

type IndexQuery = {
  searchParam: string;
  pageNumber: string;
};

interface StatusChatEndData {
  name: string;
  farewellMessage: string;
  isDefault: boolean;
}

import ListStatusChatEndService from "../services/StatusChatEndService/ListStatusChatEndService";
import ShowStatusChatEndService from "../services/StatusChatEndService/ShowStatusChatEndService";
import UpdateStatusChatEndService from "../services/StatusChatEndService/UpdateStatusChatEndService";
import { getIO } from "../libs/socket";
import StatusChatEnd from "../models/StatusChatEnd";
import CreateStatusChatEndService from "../services/StatusChatEndService/CreateStatusChatEndService";
import { del } from "../helpers/RedisClient";

// export const show = async (req: Request, res: Response): Promise<Response> => {

//     const { statusChatEnd, count, hasMore } = await ListStatusChatEndService({ searchParam: "", pageNumber: "1" });

//     return res.status(200).json(statusChatEnd);
// };

export const index = async (req: Request, res: Response): Promise<Response> => {
  const { searchParam, pageNumber } = req.query as IndexQuery;

  const { statusChatEnd, count, hasMore } = await ListStatusChatEndService({
    searchParam,
    pageNumber
  });

  return res.json({ statusChatEnd, count, hasMore });
};

export const show = async (req: Request, res: Response): Promise<Response> => {
  const { statusChatEndId } = req.params;

  let statushCatEnd;

  const isNumber = (str: any) => !isNaN(str);

  if (isNumber(statusChatEndId))
    statushCatEnd = await ShowStatusChatEndService({ id: statusChatEndId });
  else statushCatEnd = await ShowStatusChatEndService({ isDefault: true });

  return res.status(200).json(statushCatEnd);
};

export const store = async (req: Request, res: Response): Promise<Response> => {
  const newStatusChatEnd: StatusChatEndData = req.body;

  const StatusChatEndSchema = Yup.object().shape({
    name: Yup.string().required()
  });

  try {
    await StatusChatEndSchema.validate(newStatusChatEnd);
  } catch (err: any) {
    throw new AppError(err.message);
  }

  const statusChatEnd = await CreateStatusChatEndService({
    ...newStatusChatEnd
  });

  const io = getIO();
  io.emit("statusChatEnd", {
    action: "create",
    statusChatEnd
  });

  return res.status(200).json(statusChatEnd);
};

export const update = async (
  req: Request,
  res: Response
): Promise<Response> => {
  const statusChatEndData: StatusChatEndData = req.body;

  const schema = Yup.object().shape({
    name: Yup.string()
  });

  try {
    await schema.validate(statusChatEndData);
  } catch (err: any) {
    throw new AppError(err.message);
  }

  const { statusChatEndId } = req.params;

  const statusChatEnd = await UpdateStatusChatEndService({
    statusChatEndData,
    statusChatEndId
  });

  const io = getIO();
  io.emit("statusChatEnd", {
    action: "update",
    statusChatEnd
  });

  return res.status(200).json(statusChatEnd);
};

export const remove = async (
  req: Request,
  res: Response
): Promise<Response> => {
  const { statusChatEndId } = req.params;

  await StatusChatEnd.destroy({ where: { id: statusChatEndId } });

  await del(`statusChatEnd:${statusChatEndId}`);

  const io = getIO();
  io.emit("statusChatEnd", {
    action: "delete",
    statusChatEndId
  });

  return res.status(200).json({ message: "Status chat deleted" });
};
