package workspace

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/dig"

	"github.com/tidbcloud/aisupport/packages/sdk-go/infra/apiserver"
	"github.com/tidbcloud/aisupport/services/ticket-manager/internal/app"
	workspacesvcapi "github.com/tidbcloud/aisupport/services/ticket-manager/internal/services/workspace/api"
)

type HandlerParam struct {
	dig.In

	Workspace workspacesvcapi.Service
}

type Handler struct {
	Workspace workspacesvcapi.Service
}

func NewHandler(param HandlerParam) app.HandlerResult {
	return app.HandlerResult{
		Handler: &Handler{
			Workspace: param.Workspace,
		},
	}
}

func (h *Handler) Name() string {
	return "WorkspaceHandler"
}

func (h *Handler) Register(routers map[apiserver.RoutePath]gin.IRouter) error {
	router := routers[app.APIRoutePath]

	g := router.Group("/workspace")
	{
		g.GET("/overview", h.GetOverview)
		g.GET("/workspaces", h.ListWorkspaces)
		g.POST("/workspaces", h.InitializeWorkspace)
		g.GET("/workspaces/:id", h.GetWorkspace)
		g.PUT("/workspaces/:id", h.UpdateWorkspace)
		g.POST("/workspaces/:id/recover", h.RecoverWorkspace)
		g.POST("/workspaces/:id/archive", h.ArchiveWorkspace)
		g.POST("/case-workspaces/:issue_key/sync", h.SyncCaseWorkspaceOfIssue)

		g.GET("/workspaces/:id/tasks", h.ListTasks)
		g.POST("/workspaces/:id/tasks", h.CreateTask)
		g.GET("/workspaces/:id/tasks/:task_id", h.GetTask)
		g.PUT("/workspaces/:id/tasks/:task_id", h.UpdateTask)
		g.DELETE("/workspaces/:id/tasks/:task_id", h.DeleteTask)
		g.POST("/workspaces/:id/tasks/:task_id/complete", h.CompleteTask)

		g.GET("/workspaces/:id/curated-timeline", h.ListCuratedTimeline)
		g.POST("/workspaces/:id/curated-timeline", h.CreateCuratedTimelineEntry)
		g.PATCH("/workspaces/:id/timeline-summary", h.UpdateTimelineSummary)
		g.GET("/workspaces/:id/timeline-evidence", h.ListTimelineEvidence)
		g.POST("/workspaces/:id/timeline-sync", h.SyncWorkspaceTimeline)

		g.PATCH("/curated-timeline/:entry_id", h.UpdateCuratedTimelineEntry)
		g.POST("/curated-timeline/:entry_id/confirm", h.ConfirmCuratedTimelineEntry)
		g.POST("/curated-timeline/:entry_id/hide", h.HideCuratedTimelineEntry)
		g.POST("/curated-timeline/:entry_id/evidence", h.BindTimelineEvidence)

		g.GET("/issue-bindings", h.ListIssueBindings)
		g.POST("/issue-bindings", h.CreateIssueBindings)
		g.DELETE("/issue-bindings", h.DeleteIssueBindings)

		g.GET("/alert-bindings", h.ListAlertBindings)
		g.POST("/alert-bindings", h.CreateAlertBindings)
		g.DELETE("/alert-bindings", h.DeleteAlertBindings)

		g.GET("/tenant-bindings", h.ListTenantBindings)
		g.POST("/tenant-bindings", h.CreateTenantBindings)
		g.DELETE("/tenant-bindings", h.DeleteTenantBindings)
		g.GET("/cluster-bindings", h.ListClusterBindings)
		g.POST("/cluster-bindings", h.CreateClusterBindings)
		g.DELETE("/cluster-bindings", h.DeleteClusterBindings)

		g.POST("/collaboration-resource", h.BindCollaborationResource)
		g.GET("/collaboration-resource", h.ListCollaborationResources)
		g.DELETE("/collaboration-resource", h.UnbindCollaborationResource)

		g.GET("/advices", h.ListAdvices)
		g.POST("/advices", h.CreateAdvice)
		g.GET("/advices/:id", h.GetAdvice)
		g.PUT("/advices/:id", h.UpdateAdvice)
		g.POST("/advices/:id/apply", h.ApplyAdvice)
		g.POST("/advices/:id/ignore", h.IgnoreAdvice)
		g.DELETE("/advices/:id", h.DeleteAdvice)
	}

	return nil
}
