package robot

import (
	"jannex/robot-control-manager/modules/cache"
	"jannex/robot-control-manager/modules/structs"
	"time"

	"git.ex.umbach.dev/Alex/roese-utils/rsutils"
	"github.com/gofiber/fiber/v2"
	"github.com/rs/zerolog/log"
)

func FirstRequest(c *fiber.Ctx) error {
	// swagger:operation POST /robots robots robotsFirstRequest
	// ---
	// summary: First request from robot.
	// description: |
	//   This is the first request from the robot. It will be used to identify the robot.
	// consumes:
	// - application/json
	// produces:
	// - application/json
	// parameters:
	// - in: body
	//   name: body
	//   description: First request body.
	//   required: true
	//   schema:
	//     "$ref": "#/definitions/FirstRequestBody"
	// responses:
	//   "200":
	//    description: OK
	//    schema:
	//     "$ref": "#/definitions/StatusResponse"

	var body structs.FirstRequestBody

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

	cache.AddRobot(&structs.Robot{
		Id:          body.Id,
		Type:        body.Type,
		Address:     c.IP(),
		ConnectedAt: time.Now(),
	})

	log.Info().Msgf("Added robot %s (%v). Robots %v", body.Id, body.Type, cache.GetRobots())

	return c.JSON(structs.StatusResponse{Status: "ok"})
}
