package logger

import (
	"jannex/admin-dashboard-backend/modules/logger"
	"jannex/admin-dashboard-backend/modules/structs"
	"jannex/admin-dashboard-backend/modules/utils"
	"jannex/admin-dashboard-backend/socketclients"

	"github.com/gofiber/fiber/v2"
)

func GetSystemLog(c *fiber.Ctx) error {
	t := c.Query("type")
	date := c.Query("date")
	lang := c.Query("lang")

	if t == "" || date == "" || lang == "" {
		return c.SendStatus(fiber.StatusBadRequest)
	}

	if t == "g" && !socketclients.HasPermission(c.Locals("userId").(string), utils.PermissionGroupTasksHistory) || t == "s" && !socketclients.HasPermission(c.Locals("userId").(string), utils.PermissionAdminAreaLogs) {
		return c.SendStatus(fiber.StatusUnauthorized)
	}

	logType := "Group Tasks"

	if t == "s" {
		logType = "System"
	}

	logger.AddSystemLog(structs.LogMessage{
		Id:   17,
		Type: utils.LogTypeInfo,
		Messages: []structs.LogData{
			{Type: "userId", Value: c.Locals("userId").(string)},
			{Type: "logType", Value: logType},
			{Type: "logDate", Value: date},
		},
	})

	if t == "g" { // grouptasks logs
		return c.JSON(struct {
			Logs  []structs.LogMessageResponse
			Dates []string
		}{
			Logs:  logger.ReadLogs(date, false, lang),
			Dates: logger.GetAllLogMessagesDates(false),
		})
	}

	// system logs
	return c.JSON(struct {
		Logs  []structs.LogMessageResponse
		Dates []string
	}{
		Logs:  logger.ReadLogs(date, true, lang),
		Dates: logger.GetAllLogMessagesDates(true),
	})
}
