package controllers

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

type LegBrandController struct {
	queries *catalogue.Queries
}

func NewLegBrandController(queries *catalogue.Queries) *LegBrandController {
	return &LegBrandController{queries: queries}
}

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

func (ctrl *LegBrandController) GetLegBrandById(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.GetLegBrandById(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 CreateLegBrandRequest struct {
	Name    string `json:"name" binding:"required"`
	Brand   string `json:"brand" binding:"required"`
	ImgLogo string `json:"img_logo"`
}

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

	brand, err := ctrl.queries.CreateLegBrand(c.Request.Context(), catalogue.CreateLegBrandParams{
		Name:    pgtype.Text{String: req.Name, Valid: req.Name != ""},
		Brand:   pgtype.Text{String: req.Brand, Valid: req.Brand != ""},
		ImgLogo: pgtype.Text{String: req.ImgLogo, Valid: req.ImgLogo != ""},
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusCreated, brand)
}

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

func (ctrl *LegBrandController) UpdateLegBrand(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 UpdateLegBrandRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

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

func (ctrl *LegBrandController) DeleteLegBrand(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.DeleteLegBrand(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"})
}
