package controllers

import (
	"catalogue.midpdsa.com/src/db/catalogue"
	"github.com/gin-gonic/gin"
	"github.com/jackc/pgx/v5/pgtype"
	"net/http"
	"strconv"
)

type StrutBrandController struct {
	queries *catalogue.Queries
}

func NewStrutBrandController(queries *catalogue.Queries) *StrutBrandController {
	return &StrutBrandController{queries: queries}
}

func (ctrl *StrutBrandController) GetStrutBrands(c *gin.Context) {
	limit, offset := parsePagination(c)
	brands, err := ctrl.queries.GetStrutBrands(c.Request.Context(), catalogue.GetStrutBrandsParams{Limit: limit, Offset: offset})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, brands)
}

func (ctrl *StrutBrandController) GetStrutBrandById(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid brand id"})
		return
	}

	brand, err := ctrl.queries.GetStrutBrandById(c.Request.Context(), int32(id))
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "brand not found"})
		return
	}
	c.JSON(http.StatusOK, brand)
}

type CreateStrutBrandRequest struct {
	Name    string `json:"name" binding:"required"`
	Brand   string `json:"brand" binding:"required"`
	ImgLogo string `json:"img_logo"`
}

func (ctrl *StrutBrandController) CreateStrutBrand(c *gin.Context) {
	var req CreateStrutBrandRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	var imgLogo pgtype.Text
	if req.ImgLogo != "" {
		imgLogo = pgtype.Text{String: req.ImgLogo, Valid: true}
	}

	brand, err := ctrl.queries.CreateStrutBrand(c.Request.Context(), catalogue.CreateStrutBrandParams{
		Name:    req.Name,
		Brand:   req.Brand,
		ImgLogo: imgLogo,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusCreated, brand)
}

type UpdateStrutBrandRequest struct {
	Name    string `json:"name" binding:"required"`
	Brand   string `json:"brand" binding:"required"`
	ImgLogo string `json:"img_logo"`
}

func (ctrl *StrutBrandController) UpdateStrutBrand(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid brand id"})
		return
	}

	var req UpdateStrutBrandRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	var imgLogo pgtype.Text
	if req.ImgLogo != "" {
		imgLogo = pgtype.Text{String: req.ImgLogo, Valid: true}
	}

	brand, err := ctrl.queries.UpdateStrutBrand(c.Request.Context(), catalogue.UpdateStrutBrandParams{
		ID:      int32(id),
		Name:    req.Name,
		Brand:   req.Brand,
		ImgLogo: imgLogo,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, brand)
}

func (ctrl *StrutBrandController) DeleteStrutBrand(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid brand id"})
		return
	}

	_, err = ctrl.queries.DeleteStrutBrand(c.Request.Context(), int32(id))
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "brand deleted"})
}
