package handlers

import (
	"net/http"

	"github.com/breakpilot/ai-compliance-sdk/internal/iace"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

// ============================================================================
// Production Line Handlers
// ============================================================================

// CreateProductionLine handles POST /iace/production-lines
// Creates a new production line for the authenticated tenant.
func (h *IACEHandler) CreateProductionLine(c *gin.Context) {
	tenantID, err := getTenantID(c)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

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

	line, err := h.store.CreateProductionLine(c.Request.Context(), tenantID, req.Name, req.Description)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, gin.H{"line": line})
}

// ListProductionLines handles GET /iace/production-lines
// Lists all production lines for the authenticated tenant.
func (h *IACEHandler) ListProductionLines(c *gin.Context) {
	tenantID, err := getTenantID(c)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	lines, err := h.store.ListProductionLines(c.Request.Context(), tenantID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	if lines == nil {
		lines = []iace.ProductionLine{}
	}

	c.JSON(http.StatusOK, iace.ProductionLineListResponse{
		Lines: lines,
		Total: len(lines),
	})
}

// GetProductionLineDashboard handles GET /iace/production-lines/:lid/dashboard
// Returns the aggregated risk dashboard for a production line.
func (h *IACEHandler) GetProductionLineDashboard(c *gin.Context) {
	lineID, err := uuid.Parse(c.Param("lid"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid line ID"})
		return
	}

	dashboard, err := h.store.GetLineDashboard(c.Request.Context(), lineID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	if dashboard == nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "production line not found"})
		return
	}

	c.JSON(http.StatusOK, dashboard)
}

// AddStationToLine handles POST /iace/production-lines/:lid/stations
// Adds a station (IACE project reference) to a production line.
func (h *IACEHandler) AddStationToLine(c *gin.Context) {
	lineID, err := uuid.Parse(c.Param("lid"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid line ID"})
		return
	}

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

	// Verify the production line exists
	line, err := h.store.GetProductionLine(c.Request.Context(), lineID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	if line == nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "production line not found"})
		return
	}

	// Verify the referenced project exists
	project, err := h.store.GetProject(c.Request.Context(), req.ProjectID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	if project == nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "referenced project not found"})
		return
	}

	station, err := h.store.AddStation(
		c.Request.Context(), lineID, req.ProjectID,
		req.StationType, req.StationLabel, req.SortOrder,
	)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, gin.H{"station": station})
}

// RemoveStationFromLine handles DELETE /iace/production-lines/:lid/stations/:sid
// Removes a station from a production line.
func (h *IACEHandler) RemoveStationFromLine(c *gin.Context) {
	_, err := uuid.Parse(c.Param("lid"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid line ID"})
		return
	}

	stationID, err := uuid.Parse(c.Param("sid"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid station ID"})
		return
	}

	if err := h.store.RemoveStation(c.Request.Context(), stationID); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "station removed"})
}
