package equipment

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

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

func CreateEquipmentDocumentation(c *fiber.Ctx) error {
	// swagger:operation POST /equipment/documentation/create equipment equipmentCreateEquipmentDocumentation
	// ---
	// summary: Create a new equipment documentation
	// consumes:
	// - application/json
	// produces:
	// - application/json
	// parameters:
	// - name: X-Api-Key
	//   in: header
	//   description: You can create a new api key in your user profile
	// - name: body
	//   in: body
	//   schema:
	//     "$ref": "#/definitions/CreateEquipmentDocumentationRequest"
	// responses:
	//   '200':
	//     description: New equipment documentation created successfully
	//   '400':
	//     description: Invalid request body
	//   '401':
	//     description: No permissions
	//   '500':
	//     description: Failed to create equipment documentation

	if !socketclients.HasPermission(c.Locals("userId").(string), utils.PermissionEquipmentDocumentationCreate) {
		return c.SendStatus(fiber.StatusUnauthorized)
	}

	var body structs.CreateEquipmentDocumentationRequest

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

	return equipment.CreateEquipmentDocumentation(c, body)
}

func GetEquipmentDocumentations(c *fiber.Ctx) error {
	// swagger:operation GET /equipment/documentations/{stockItemId} equipment equipmentGetEquipmentDocumentations
	// ---
	// summary: Get equipment documentations
	// consumes:
	// - application/json
	// produces:
	// - application/json
	// parameters:
	// - name: X-Api-Key
	//   in: header
	//   description: You can create a new api key in your user profile
	// - name: stockItemId
	//   in: path
	//   description: Stock item id
	//   required: true
	//   type: string
	// responses:
	//   '200':
	//     description: Equipment documentations. Status 200 if equipment documentations found, status 404 if stock item not found on invex, status 401 if backend has no permissions to access invex
	//     schema:
	//       "$ref": "#/definitions/EquipmentDocumentationResponse"
	//   '400':
	//     description: Invalid request body
	//   '401':
	//     description: No permissions
	//   '500':
	//     description: Failed to get equipment documentations

	if !socketclients.HasPermission(c.Locals("userId").(string), utils.PermissionEquipmentDocumentationView) {
		return c.SendStatus(fiber.StatusUnauthorized)
	}

	var params structs.EquipmentRequest

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

	return equipment.GetEquipmentDocumentations(params.StockItemId, c)
}

func GetEquipmentDocumentation(c *fiber.Ctx) error {
	// swagger:operation GET /equipment/documentation/{stockItemId}/{documentationId} equipment equipmentGetEquipmentDocumentation
	// ---
	// summary: Get equipment documentation
	// consumes:
	// - application/json
	// produces:
	// - application/json
	// parameters:
	// - name: X-Api-Key
	//   in: header
	//   description: You can create a new api key in your user profile
	// - name: stockItemId
	//   in: path
	//   description: Stock item id
	//   required: true
	//   type: string
	// - name: documentationId
	//   in: path
	//   description: Documentation id
	//   required: true
	//   type: string
	// responses:
	//   '200':
	//     description: Equipment documentation
	//     schema:
	//       "$ref": "#/definitions/EquipmentDocumentation"
	//   '400':
	//     description: Invalid request body
	//   '401':
	//     description: No permissions
	//   '500':
	//     description: Failed to get equipment documentation

	if !socketclients.HasPermission(c.Locals("userId").(string), utils.PermissionEquipmentDocumentationView) {
		return c.SendStatus(fiber.StatusUnauthorized)
	}

	var params structs.GetDocumentationEquipmentRequest

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

	return equipment.GetEquipmentDocumentation(params.StockItemId, params.DocumentationId, c)
}

func EditEquipmentDocumentation(c *fiber.Ctx) error {
	// swagger:operation POST /equipment/documentation/edit equipment equipmentEditEquipmentDocumentation
	// ---
	// summary: Edit equipment documentation
	// consumes:
	// - application/json
	// produces:
	// - application/json
	// parameters:
	// - name: X-Api-Key
	//   in: header
	//   description: You can create a new api key in your user profile
	// - name: body
	//   in: body
	//   schema:
	//     "$ref": "#/definitions/EditEquipmentDocumentationRequest"
	// responses:
	//   '200':
	//     description: Equipment documentation edited successfully
	//   '400':
	//     description: Invalid request body
	//   '401':
	//     description: No permissions
	//   '500':
	//     description: Failed to edit equipment documentation

	if !socketclients.HasPermission(c.Locals("userId").(string), utils.PermissionEquipmentDocumentationEdit) {
		return c.SendStatus(fiber.StatusUnauthorized)
	}

	var body structs.EditEquipmentDocumentationRequest

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

	return equipment.EditEquipmentDocumentation(c, body)
}

func GetEquipmentThumbnail(c *fiber.Ctx) error {
	// swagger:operation GET /equipment/thumbnail/{stockItemId} equipment equipmentGetEquipmentThumbnail
	// ---
	// summary: Get equipment thumbnail
	// consumes:
	// - application/json
	// produces:
	// - application/json
	// parameters:
	// - name: stockItemId
	//   in: path
	//   description: Stock item id
	//   required: true
	//   type: string
	// responses:
	//   '200':
	//     description: Equipment thumbnail picture. Backend server is just proxying the request to invex server and returning the response
	//   '400':
	//     description: Invalid request body
	//   '401':
	//     description: No permissions
	//   '500':
	//     description: Failed to get equipment thumbnail

	var params structs.EquipmentRequest

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

	return equipment.GetEquipmentInvexThumbnail(c, params.StockItemId)
}
