package equipment

import (
	"jannex/admin-dashboard-backend/modules/database"
	"jannex/admin-dashboard-backend/modules/structs"

	"github.com/gofiber/fiber/v2"
	"github.com/rs/zerolog/log"
)

const Base = "https://inv.ex.umbach.dev"
const apiBase = Base + "/api"
const ApiToken = "1367f15d21935e4eb540f897946fb5cd98485c3f"

func GetEquipmentDocumentation(stockItemId string, c *fiber.Ctx) error {
	var documentations []structs.EquipmentDocumentation

	database.DB.Where("stock_item_id = ?", stockItemId).Find(&documentations)

	statusCode := 200

	if len(documentations) == 0 {
		// there are no documentations for this equipment on the our database
		// so there will be checked on invex if the stock item exists

		a := fiber.AcquireAgent()

		a.Add("Authorization", "Token "+ApiToken)

		req := a.Request()
		req.Header.SetMethod(fiber.MethodGet)
		req.SetRequestURI(apiBase + "/stock/" + stockItemId + "/")

		if err := a.Parse(); err != nil {
			log.Error().Msgf("Failed to parse request, err: %s", err)
			return c.SendStatus(fiber.StatusInternalServerError)
		}

		statusCode, _, _ = a.Bytes()

		if statusCode == 401 {
			log.Error().Msgf("invex not authorized, statusCode: %d", statusCode)
		}

		if statusCode == 404 {
			log.Error().Msgf("Invex stock item not found, statusCode: %d", statusCode)
		}
	}

	return c.JSON(structs.ApiEquipmentDocumentationResponse{
		Status:         statusCode,
		Documentations: documentations})
}

/*
func GetEquipmentDocumentation(stockItemId string, c *fiber.Ctx) error {
	equipment := structs.Equipment{Id: stockItemId}

	if err := database.DB.Where("id = ?", stockItemId).First(&equipment).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		log.Info().Msgf("a %s", err)

		log.Error().Msgf("Failed to get equipment, err: %s", err)
		return c.JSON([]structs.EquipmentDocumentation{})

	}

	// if equipment already exists, return the documentation
	if equipment.Name != "" {
		var equipmentDocumentations []structs.EquipmentDocumentation

		database.DB.Where("equipment_id = ?", stockItemId).Find(&equipmentDocumentations)

		return c.JSON(equipmentDocumentations)
	}

	// create new equipment
	a := fiber.AcquireAgent()

	a.Add("Authorization", "Token "+ApiToken)

	req := a.Request()
	req.Header.SetMethod(fiber.MethodGet)
	req.SetRequestURI(apiBase + "/stock/" + stockItemId + "/")

	if err := a.Parse(); err != nil {
		log.Error().Msgf("Failed to parse request, err: %s", err)
		return c.SendStatus(fiber.StatusInternalServerError)
	}

	code, body, _ := a.Bytes()

	if code == 401 {
		log.Error().Msgf("invex not authorized, code: %d", code)
		return c.SendStatus(fiber.StatusInternalServerError)
	}

	if code == 404 {
		log.Error().Msgf("Inven stock item not found, code: %d", code)
		return c.SendStatus(fiber.StatusNotFound)
	}

	// parse body as json
	var data map[string]interface{}

	if err := json.Unmarshal(body, &data); err != nil {
		log.Error().Msgf("Failed to unmarshal json, err: %s", err)
		return c.SendStatus(fiber.StatusInternalServerError)
	}

	partDetail := data["part_detail"].(map[string]interface{})

	database.DB.Create(&structs.Equipment{
		Id:        stockItemId,
		Name:      partDetail["name"].(string),
		Thumbnail: partDetail["thumbnail"].(string),
	})

	return c.JSON([]structs.EquipmentDocumentation{})
}
*/

/*
func GetEquipment() []structs.Equipment {
	var equipments []structs.Equipment

	database.DB.Find(&equipments)

	return equipments
}*/

/*
// return whether the scanned equipment is existing in the database
func IsEquipmentExisting(stockItemId string) bool {
	var equipment structs.Equipment

	database.DB.Where("id = ?", stockItemId).FirstOrCreate(&equipment)

	return equipment.Id != ""
} */
