package dispatch

import (
	"net/http"
	"strings"
	"time"

	"github.com/gin-gonic/gin"

	dispatchapi "github.com/tidbcloud/aisupport/packages/sdk-go/contracts/api/ticket-manager/dispatch"
	apiutils "github.com/tidbcloud/aisupport/packages/sdk-go/infra/apiserver/utils"
	"github.com/tidbcloud/aisupport/packages/sdk-go/infra/errs"
)

// ListProjectComponents lists the current Jira component candidates for a project.
//
//	@Summary	List Jira project components
//	@Id			DispatchHandlerListProjectComponents
//	@Tags		DispatchHandler
//	@Produce	json
//	@Param		project_key	path		string	true	"Jira project key"
//	@Success	200			{object}	dispatchapi.ListProjectComponentsResponse
//	@Failure	default		{object}	base.BaseResponse
//	@Router		/api/v1/ticketmanager/dispatch/projects/{project_key}/components [get]
func (h *Handler) ListProjectComponents(c *gin.Context) {
	projectKey := strings.TrimSpace(c.Param("project_key"))
	if projectKey == "" {
		apiutils.Fail(c, errs.APIErrorf(http.StatusBadRequest, "project_key is required"))
		return
	}
	components, err := h.Dispatch.ListProjectComponents(c, projectKey)
	if err != nil {
		apiutils.Fail(c, err)
		return
	}
	apiutils.Success(c, &dispatchapi.ListProjectComponentsResponse{
		ProjectKey: projectKey,
		Components: components,
		FetchedAt:  time.Now().UTC(),
	})
}
