package controllers

import (
	"math"
	"strconv"

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

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

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

func GetConxDb(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.GetAllConxDb(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 CreateConxDb(c *fiber.Ctx) error {
	var m models.ConxDb

	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.CreateConxDb(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 UpdateConxDb(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.ConxDb

	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.UpdateConxDb(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 DeleteConxDb(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.ConxDb
	m.ID = uint(uid)

	if err := models.DeleteConxDb(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",
	})
}

func GetConxDbSelect(c *fiber.Ctx) error {
	servidorID := c.Query("servidor_id")
	items, err := models.GetConxDbSelect(servidorID)
	if err != nil {
		return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
	}
	return c.JSON(fiber.Map{"registros": items})
}
