package controllers

import (
	"math"
	"strconv"

	"github.com/gofiber/fiber/v2"
	"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
)

// RENDER
func ConxSsh(c *fiber.Ctx) error {
	data := fiber.Map{
		"user":    c.Locals("user").(map[string]interface{}),
		"modules": c.Locals("userModules"),
	}

	if err := c.Render("conx_ssh", data, "layouts/main"); err != nil {
		return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
			"message": "Error rendering template",
			"error":   err.Error(),
		})
	}
	return nil
}

func GetConxSsh(c *fiber.Ctx) error {
	pageStr := c.Query("page", "1")
	page, err := strconv.Atoi(pageStr)
	if err != nil || page < 1 {
		page = 1
	}
	searchQuery := c.Query("search", "")
	limitStr := c.Query("limit", "10")
	limit, _ := strconv.Atoi(limitStr)
	offset := (page - 1) * limit

	records, total, err := models.GetAllConxSsh(limit, offset, searchQuery)
	if err != nil {
		return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
			"message": "Error retrieving records",
			"error":   err.Error(),
		})
	}
	totalPages := int(math.Ceil(float64(total) / float64(limit)))
	return c.JSON(fiber.Map{
		"registros":  records,
		"total":      total,
		"totalPages": totalPages,
		"page":       page,
		"limit":      limit,
	})
}

func CreateConxSsh(c *fiber.Ctx) error {
	var m models.ConxSsh

	if err := c.BodyParser(&m); err != nil {
		return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
			"message": "Error parsing request body",
			"error":   err.Error(),
		})
	}

	if err := models.CreateConxSsh(m); err != nil {
		return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
			"message": "Error creating conx ssh",
			"error":   err.Error(),
		})
	}

	return c.Status(fiber.StatusCreated).JSON(m)
}

func UpdateConxSsh(c *fiber.Ctx) error {
	uid, err := strconv.ParseUint(c.Params("id"), 10, 32)
	if err != nil {
		return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
			"message": "ID inválido",
			"error":   err.Error(),
		})
	}

	var m models.ConxSsh

	if err := c.BodyParser(&m); err != nil {
		return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
			"message": "Error al parsear el cuerpo de la solicitud",
			"error":   err.Error(),
		})
	}

	m.ID = uint(uid)

	if err := models.UpdateConxSsh(m); err != nil {
		return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
			"message": "Error al actualizar el conx ssh",
			"error":   err.Error(),
		})
	}

	return c.Status(fiber.StatusOK).JSON(m)
}

func DeleteConxSsh(c *fiber.Ctx) error {
	uid, err := strconv.ParseUint(c.Params("id"), 10, 32)
	if err != nil {
		return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
			"message": "ID inválido",
			"error":   err.Error(),
		})
	}

	var m models.ConxSsh
	m.ID = uint(uid)

	if err := models.DeleteConxSsh(m); err != nil {
		return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
			"message": "Error al eliminar el conx ssh",
			"error":   err.Error(),
		})
	}

	return c.Status(fiber.StatusOK).JSON(fiber.Map{
		"message": "conx ssh eliminado exitosamente",
	})
}
