package controllers

import (
	"fotbal-club/internal/models"
	"net/http"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// ClothingController handles clothing/merch endpoints
type ClothingController struct {
	DB *gorm.DB
}

// NewClothingController creates a new clothing controller
func NewClothingController(db *gorm.DB) *ClothingController {
	return &ClothingController{DB: db}
}

// GetClothing retrieves all active clothing items (public endpoint)
func (ctrl *ClothingController) GetClothing(c *gin.Context) {
	db := ctrl.DB
	
	var items []models.Clothing
	if err := db.Where("is_active = ?", true).Order("display_order ASC, created_at DESC").Find(&items).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch clothing items"})
		return
	}
	
	c.JSON(http.StatusOK, gin.H{"data": items})
}

// GetClothingAdmin retrieves all clothing items for admin
func (ctrl *ClothingController) GetClothingAdmin(c *gin.Context) {
	db := ctrl.DB
	
	var items []models.Clothing
	if err := db.Order("display_order ASC, created_at DESC").Find(&items).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch clothing items"})
		return
	}
	
	c.JSON(http.StatusOK, gin.H{"data": items})
}

// GetClothingByID retrieves a single clothing item by ID
func (ctrl *ClothingController) GetClothingByID(c *gin.Context) {
	db := ctrl.DB
	id := c.Param("id")
	
	var item models.Clothing
	if err := db.First(&item, id).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			c.JSON(http.StatusNotFound, gin.H{"error": "Clothing item not found"})
			return
		}
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch clothing item"})
		return
	}
	
	c.JSON(http.StatusOK, item)
}

// CreateClothing creates a new clothing item
func (ctrl *ClothingController) CreateClothing(c *gin.Context) {
	db := ctrl.DB
	
	var input models.Clothing
	if err := c.ShouldBindJSON(&input); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	
	if err := db.Create(&input).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create clothing item"})
		return
	}
	
	c.JSON(http.StatusCreated, input)
}

// UpdateClothing updates a clothing item
func (ctrl *ClothingController) UpdateClothing(c *gin.Context) {
	db := ctrl.DB
	id := c.Param("id")
	
	var item models.Clothing
	if err := db.First(&item, id).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			c.JSON(http.StatusNotFound, gin.H{"error": "Clothing item not found"})
			return
		}
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch clothing item"})
		return
	}
	
	var input models.Clothing
	if err := c.ShouldBindJSON(&input); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	
	// Update fields
	item.Title = input.Title
	item.Description = input.Description
	item.Price = input.Price
	item.Currency = input.Currency
	item.ImageURL = input.ImageURL
	item.URL = input.URL
	item.IsActive = input.IsActive
	item.DisplayOrder = input.DisplayOrder
	
	if err := db.Save(&item).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update clothing item"})
		return
	}
	
	c.JSON(http.StatusOK, item)
}

// DeleteClothing deletes a clothing item (soft delete)
func (ctrl *ClothingController) DeleteClothing(c *gin.Context) {
	db := ctrl.DB
	id := c.Param("id")
	
	var item models.Clothing
	if err := db.First(&item, id).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			c.JSON(http.StatusNotFound, gin.H{"error": "Clothing item not found"})
			return
		}
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch clothing item"})
		return
	}
	
	if err := db.Delete(&item).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete clothing item"})
		return
	}
	
	c.JSON(http.StatusOK, gin.H{"message": "Clothing item deleted"})
}

// UpdateClothingOrder updates the display order of clothing items
func (ctrl *ClothingController) UpdateClothingOrder(c *gin.Context) {
	db := ctrl.DB
	
	var input []struct {
		ID           uint `json:"id"`
		DisplayOrder int  `json:"display_order"`
	}
	
	if err := c.ShouldBindJSON(&input); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	
	// Update each item's display order
	for _, item := range input {
		if err := db.Model(&models.Clothing{}).Where("id = ?", item.ID).Update("display_order", item.DisplayOrder).Error; err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update order"})
			return
		}
	}
	
	c.JSON(http.StatusOK, gin.H{"message": "Order updated"})
}
