package controller

import (
	"encoding/json"
	"jaecoo-be/app/module/products/request"
	"jaecoo-be/app/module/products/service"
	"jaecoo-be/utils/paginator"
	"strconv"

	"github.com/gofiber/fiber/v2"

	utilRes "jaecoo-be/utils/response"
)

type productsController struct {
	productsService service.ProductsService
}

type ProductsController 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 NewProductsController(productsService service.ProductsService) ProductsController {
	return &productsController{
		productsService: productsService,
	}
}

// All Products
// @Summary      Get all Products
// @Description  API for getting all Products
// @Tags         Products
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param 	     req query request.ProductsQueryRequestContext 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       /products [get]
func (_i *productsController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.ProductsQueryRequestContext{
		Title:   c.Query("title"),
		Variant: c.Query("variant"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

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

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

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

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

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

// Save Product
// @Summary      Create Product
// @Description  API for creating Product with file upload
// @Tags         Products
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param        file formData file false "Upload file"
// @Param        title formData string true "Product title"
// @Param        variant formData string false "Product variant"
// @Param        price formData string false "Product price"
// @Param        colors formData string false "Product colors (JSON array)"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /products [post]
func (_i *productsController) 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
	req := request.ProductsCreateRequest{
		Title: c.FormValue("title"),
	}

	if variant := c.FormValue("variant"); variant != "" {
		req.Variant = &variant
	}

	if price := c.FormValue("price"); price != "" {
		req.Price = &price
	}

	// Handle colors (JSON array string)
	if colorsStr := c.FormValue("colors"); colorsStr != "" {
		var colors []string
		if err := json.Unmarshal([]byte(colorsStr), &colors); err == nil {
			req.Colors = colors
		}
	}

	// 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.productsService.Create(c, req)
	if err != nil {
		return err
	}

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

// Update Product
// @Summary      Update Product
// @Description  API for updating Product with file upload
// @Tags         Products
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param        id path int true "Product ID"
// @Param        file formData file false "Upload file"
// @Param        title formData string false "Product title"
// @Param        variant formData string false "Product variant"
// @Param        price formData string false "Product price"
// @Param        colors formData string false "Product colors (JSON array)"
// @Param        is_active formData bool false "Product is_active"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /products/{id} [put]
func (_i *productsController) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 0)
	if err != nil {
		return err
	}

	// 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
	req := request.ProductsUpdateRequest{}

	if title := c.FormValue("title"); title != "" {
		req.Title = &title
	}

	if variant := c.FormValue("variant"); variant != "" {
		req.Variant = &variant
	}

	if price := c.FormValue("price"); price != "" {
		req.Price = &price
	}

	// Handle colors (JSON array string)
	if colorsStr := c.FormValue("colors"); colorsStr != "" {
		var colors []string
		if err := json.Unmarshal([]byte(colorsStr), &colors); err == nil {
			req.Colors = colors
		}
	}

	if isActiveStr := c.FormValue("is_active"); isActiveStr != "" {
		isActive := isActiveStr == "true" || isActiveStr == "1"
		req.IsActive = &isActive
	}

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

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

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

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

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

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

// Viewer Product
// @Summary      Viewer Product
// @Description  API for viewing Product file
// @Tags         Products
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param        filename path string true "Product 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       /products/viewer/{filename} [get]
func (_i *productsController) Viewer(c *fiber.Ctx) error {
	return _i.productsService.Viewer(c)
}
