package controller

import (
	"jaecoo-be/app/module/product_specifications/request"
	"jaecoo-be/app/module/product_specifications/service"
	"jaecoo-be/utils/paginator"
	"strconv"

	"github.com/gofiber/fiber/v2"

	utilRes "jaecoo-be/utils/response"
	utilVal "jaecoo-be/utils/validator"
)

type productSpecificationsController struct {
	productSpecificationsService service.ProductSpecificationsService
}

type ProductSpecificationsController interface {
	All(c *fiber.Ctx) error
	Show(c *fiber.Ctx) error
	Save(c *fiber.Ctx) error
	Update(c *fiber.Ctx) error
	Delete(c *fiber.Ctx) error
	Viewer(c *fiber.Ctx) error
}

func NewProductSpecificationsController(productSpecificationsService service.ProductSpecificationsService) ProductSpecificationsController {
	return &productSpecificationsController{
		productSpecificationsService: productSpecificationsService,
	}
}

// All ProductSpecifications
// @Summary      Get all ProductSpecifications
// @Description  API for getting all ProductSpecifications
// @Tags         ProductSpecifications
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param 	     req query request.ProductSpecificationsQueryRequestContext false "query parameters"
// @Param 	     req query paginator.Pagination false "pagination parameters"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /product-specifications [get]
func (_i *productSpecificationsController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.ProductSpecificationsQueryRequestContext{
		ProductID: c.Query("product_id"),
		Title:     c.Query("title"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

	specsData, paging, err := _i.productSpecificationsService.GetAll(req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"ProductSpecifications list successfully retrieved"},
		Data:     specsData,
		Meta:     paging,
	})
}

// Show ProductSpecification
// @Summary      Get ProductSpecification by ID
// @Description  API for getting ProductSpecification by ID
// @Tags         ProductSpecifications
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param        id path int true "ProductSpecification ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /product-specifications/{id} [get]
func (_i *productSpecificationsController) Show(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	specData, err := _i.productSpecificationsService.GetOne(uint(id))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"ProductSpecification successfully retrieved"},
		Data:     specData,
	})
}

// Save ProductSpecification
// @Summary      Create ProductSpecification
// @Description  API for creating ProductSpecification with file upload
// @Tags         ProductSpecifications
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param        file formData file false "Upload file"
// @Param        product_id formData int true "Product ID"
// @Param        title formData string true "Product specification title"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /product-specifications [post]
func (_i *productSpecificationsController) Save(c *fiber.Ctx) error {
	// Parse multipart form
	form, err := c.MultipartForm()
	if err != nil {
		return utilRes.Resp(c, utilRes.Response{
			Success:  false,
			Messages: utilRes.Messages{"Failed to parse form data"},
		})
	}

	// Extract form values
	productIDStr := c.FormValue("product_id")
	productID, err := strconv.ParseUint(productIDStr, 10, 0)
	if err != nil {
		return utilRes.Resp(c, utilRes.Response{
			Success:  false,
			Messages: utilRes.Messages{"Invalid product_id"},
		})
	}

	req := request.ProductSpecificationsCreateRequest{
		ProductID: uint(productID),
		Title:     c.FormValue("title"),
	}

	// Validate required fields
	if req.Title == "" {
		return utilRes.Resp(c, utilRes.Response{
			Success:  false,
			Messages: utilRes.Messages{"Title is required"},
		})
	}

	// Check if file is uploaded
	if len(form.File["file"]) > 0 {
		// File will be handled in service
	}

	dataResult, err := _i.productSpecificationsService.Create(c, req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"ProductSpecification successfully created"},
		Data:     dataResult,
	})
}

// Update ProductSpecification
// @Summary      Update ProductSpecification
// @Description  API for updating ProductSpecification
// @Tags         ProductSpecifications
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param        id path int true "ProductSpecification ID"
// @Param 	     payload body request.ProductSpecificationsUpdateRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /product-specifications/{id} [put]
func (_i *productSpecificationsController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	req := new(request.ProductSpecificationsUpdateRequest)
	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	dataResult, err := _i.productSpecificationsService.Update(uint(id), *req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"ProductSpecification successfully updated"},
		Data:     dataResult,
	})
}

// Delete ProductSpecification
// @Summary      Delete ProductSpecification
// @Description  API for deleting ProductSpecification (soft delete)
// @Tags         ProductSpecifications
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param        id path int true "ProductSpecification ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /product-specifications/{id} [delete]
func (_i *productSpecificationsController) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	err = _i.productSpecificationsService.Delete(uint(id))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"ProductSpecification successfully deleted"},
	})
}

// Viewer ProductSpecification
// @Summary      Viewer ProductSpecification
// @Description  API for viewing ProductSpecification file
// @Tags         ProductSpecifications
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param        filename path string true "Product Specification File Name (e.g., user_277788.png)"
// @Success      200  {file}  file
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /product-specifications/viewer/{filename} [get]
func (_i *productSpecificationsController) Viewer(c *fiber.Ctx) error {
	return _i.productSpecificationsService.Viewer(c)
}
