package log

import (
	"fmt"
	"jannex/log-manager/modules/loghandler"
	"jannex/log-manager/modules/structs"
	"jannex/log-manager/modules/utils"

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

func AddLog(c *fiber.Ctx) error {
	// swagger:operation POST /v1/log log addLog
	// ---
	// summary: Add a log to the log file. You can specify multiple logs at once
	// consumes:
	// - application/json
	// produces:
	// - application/json
	// parameters:
	// - name: body
	//   in: body
	//   description: The log to add
	//   required: true
	//   schema:
	//     "$ref": "#/definitions/LogBody"
	// responses:
	//   '200':
	//     description: Successfully added log
	//   '400':
	//     description: Invalid request body

	var body structs.LogBody

	if err := utils.BodyParserHelper(c, &body); err != nil {
		return c.SendStatus(fiber.StatusBadRequest)
	}

	loghandler.AddLog(body)

	return c.SendStatus(fiber.StatusOK)
}

func GetLog(c *fiber.Ctx) error {
	// swagger:operation GET /v1/logs/{type} logs getLog
	// ---
	// summary: Get the log file for the specified type
	// consumes:
	// - application/json
	// produces:
	// - application/json
	// parameters:
	// - name: type
	//   in: path
	//   description: The type of log to get
	//   required: true
	//   type: string
	// - name: d
	//   in: query
	//   description: The date of the log to get
	//   required: false
	//   type: string
	// - name: f
	//   in: query
	//   description: The filter to apply to the log
	//   required: false
	//   type: string
	// responses:
	//   '200':
	//     description: Successfully got log. Returns an array of strings
	//   '400':
	//     description: Invalid request body
	//   '422':
	//     description: No log file found for the specified type or date

	var params structs.GetLogParams

	if err := utils.ParamsParserHelper(c, &params); err != nil {
		return c.SendStatus(fiber.StatusBadRequest)
	}

	var query structs.GetLogQuery

	if err := utils.QueryParserHelper(c, &query); err != nil {
		return c.SendStatus(fiber.StatusBadRequest)
	}

	fmt.Println("params", params, "query", query)

	// no type specified
	if params.Type == "" {
		return c.SendStatus(fiber.StatusBadRequest)
	}

	// no date specified -> return all available files
	if query.D == "" {
		availableLogs, err := loghandler.GetAvailableLogFiles(params.Type)

		if err != nil {
			return c.SendStatus(fiber.StatusUnprocessableEntity)
		}

		return c.JSON(availableLogs)
	}

	logs, err := loghandler.GetLogByDate(params.Type, query.D)

	if err != nil {
		return c.SendStatus(fiber.StatusUnprocessableEntity)
	}

	return c.JSON(logs)
}

func GetAvailableLogTypes(c *fiber.Ctx) error {
	// swagger:operation GET /v1/log/types log getAvailableLogTypes
	// ---
	// summary: Get the available log types
	// consumes:
	// - application/json
	// produces:
	// - application/json
	// responses:
	//   '200':
	//     description: Successfully got log types. Returns an array of strings

	logTypes := loghandler.GetAvailableLogTypes()

	return c.JSON(logTypes)
}
