package controllers

import (
	"catalogue.midpdsa.com/src/db/catalogue"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"

	"github.com/jackc/pgx/v5/pgtype"
)

type BrakeBrandController struct {
	queries *catalogue.Queries
}

func NewBrakeBrandController(queries *catalogue.Queries) *BrakeBrandController {
	return &BrakeBrandController{queries: queries}
}

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

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

func (ctrl *BrakeBrandController) CreateBrakeBrand(c *gin.Context) {
	var req CreateBrakeBrandRequest
	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.CreateBrakeBrand(c.Request.Context(), catalogue.CreateBrakeBrandParams{
		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 UpdateBrakeBrandRequest struct {
	Name    string `json:"name" binding:"required"`
	Brand   string `json:"brand" binding:"required"`
	ImgLogo string `json:"img_logo"`
}

func (ctrl *BrakeBrandController) UpdateBrakeBrand(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 UpdateBrakeBrandRequest
	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.UpdateBrakeBrand(c.Request.Context(), catalogue.UpdateBrakeBrandParams{
		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 *BrakeBrandController) DeleteBrakeBrand(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.DeleteBrakeBrand(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"})
}