import { Request, Response } from "express";
import { Testimonials } from "../../database/menu/testimonials";
import { sendSuccessResponse, sendErrorResponse } from "../../utills/response";

export const getAllTestimonials = async (req: Request, res: Response) => {
  try {
    const { page = '1', limit = '10' } = req.query;
    const pageNum = Math.max(1, parseInt(page as string, 10));
    const limitNum = Math.max(1, Math.min(100, parseInt(limit as string, 10)));
    const skip = (pageNum - 1) * limitNum;

    const [testimonials, totalCount] = await Testimonials.findAndCount({
      order: { order: "ASC" },
      skip,
      take: limitNum
    });

    const totalPages = Math.ceil(totalCount / limitNum);

    return sendSuccessResponse(res, 200, "Testimonials retrieved successfully", {
      testimonials,
      pagination: {
        currentPage: pageNum,
        totalPages,
        totalCount,
        limit: limitNum
      }
    });
  } catch (error) {
    return sendErrorResponse(res, 500, "Internal server error", error);
  }
};

export const createTestimonial = async (req: Request, res: Response) => {
  try {
    const { name, designation, testimonial, userImage, backgroundImage, order, isActive } = req.body;

    if (!name || !designation || !testimonial) {
      return sendErrorResponse(res, 400, "Name, designation and testimonial are required");
    }

    const newTestimonial = new Testimonials();
    newTestimonial.name = name;
    newTestimonial.designation = designation;
    newTestimonial.testimonial = testimonial;
    newTestimonial.userImage = userImage ?? null;
    newTestimonial.backgroundImage = backgroundImage ?? null;

    newTestimonial.order = order ?? 0;
 if (isActive !== undefined) {
  newTestimonial.isActive = isActive === false || isActive === "false" ? false : !!isActive;
  
}

    await newTestimonial.save();
    return sendSuccessResponse(res, 201, "Testimonial created successfully", newTestimonial);
  } catch (error) {
    return sendErrorResponse(res, 500, "Internal server error", error);
  }
};

export const updateTestimonial = async (req: Request, res: Response) => {
  try {
    const { id } = req.params;
    const { name, designation, testimonial, userImage, backgroundImage, order,isActive} = req.body;

    const existingTestimonial = await Testimonials.findOne({ where: { id } });
    if (!existingTestimonial) {
      return sendErrorResponse(res, 404, "Testimonial not found");
    }

    if (name) existingTestimonial.name = name;
    if (designation) existingTestimonial.designation = designation;
    if (testimonial) existingTestimonial.testimonial = testimonial;
    if (userImage !== undefined) existingTestimonial.userImage = userImage;
    if (backgroundImage !== undefined) existingTestimonial.backgroundImage = backgroundImage;
    if (order !== undefined) existingTestimonial.order = order;
   if (isActive !== undefined) {
      existingTestimonial.isActive = isActive !== "false" && !!isActive;
    }
    existingTestimonial.updated_at = new Date();

    await existingTestimonial.save();
    return sendSuccessResponse(res, 200, "Testimonial updated successfully", existingTestimonial);
  } catch (error) {
    return sendErrorResponse(res, 500, "Internal server error", error);
  }
};

export const deleteTestimonial = async (req: Request, res: Response) => {
  try {
    const { id } = req.params;

    const testimonial = await Testimonials.findOne({ where: { id } });
    if (!testimonial) {
      return sendErrorResponse(res, 404, "Testimonial not found");
    }

    await testimonial.remove();
    return sendSuccessResponse(res, 200, "Testimonial deleted successfully", null);
  } catch (error) {
    return sendErrorResponse(res, 500, "Internal server error", error);
  }
};