package schedule

import (
	"context"
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"

	"github.com/tidbcloud/AgentTeam/internal/api"
	"github.com/tidbcloud/AgentTeam/internal/schedule/manage"
)

const serviceNotConfiguredMessage = "Schedule service is not configured"

type handlerBase struct {
	scheduleService Service
}

type CollectionHandler struct{ handlerBase }
type ValidationHandler struct{ handlerBase }
type DetailHandler struct{ handlerBase }
type LifecycleHandler struct{ handlerBase }
type RunHandler struct{ handlerBase }

type routeHandlers struct {
	collection *CollectionHandler
	validation *ValidationHandler
	detail     *DetailHandler
	lifecycle  *LifecycleHandler
	runs       *RunHandler
}

func newRouteHandlers(dependencies Dependencies) routeHandlers {
	scheduleService := dependencies.ScheduleService
	if scheduleService == nil {
		scheduleService = notConfiguredService{}
	}
	base := handlerBase{scheduleService: scheduleService}
	return routeHandlers{
		collection: &CollectionHandler{handlerBase: base},
		validation: &ValidationHandler{handlerBase: base},
		detail:     &DetailHandler{handlerBase: base},
		lifecycle:  &LifecycleHandler{handlerBase: base},
		runs:       &RunHandler{handlerBase: base},
	}
}

type notConfiguredService struct{}

func (notConfiguredService) ListSchedulesForOwnerWithFilter(context.Context, string, string, manage.ListFilter) ([]manage.ScheduleView, error) {
	return nil, serviceNotConfiguredError()
}
func (notConfiguredService) ValidateScheduleForOwner(context.Context, string, string, manage.UpsertInput, string) (manage.ScheduleValidateResult, error) {
	return manage.ScheduleValidateResult{}, serviceNotConfiguredError()
}
func (notConfiguredService) CreateScheduleForOwner(context.Context, string, string, manage.UpsertInput) (manage.ScheduleView, error) {
	return manage.ScheduleView{}, serviceNotConfiguredError()
}
func (notConfiguredService) GetScheduleForOwner(context.Context, string, string, string) (manage.ScheduleView, error) {
	return manage.ScheduleView{}, serviceNotConfiguredError()
}
func (notConfiguredService) UpdateScheduleForOwner(context.Context, string, string, string, manage.UpsertInput) (manage.ScheduleView, error) {
	return manage.ScheduleView{}, serviceNotConfiguredError()
}
func (notConfiguredService) PauseScheduleForOwner(context.Context, string, string, string) (manage.ScheduleView, error) {
	return manage.ScheduleView{}, serviceNotConfiguredError()
}
func (notConfiguredService) ResumeScheduleForOwner(context.Context, string, string, string) (manage.ScheduleView, error) {
	return manage.ScheduleView{}, serviceNotConfiguredError()
}
func (notConfiguredService) DeleteScheduleForOwner(context.Context, string, string, string) error {
	return serviceNotConfiguredError()
}
func (notConfiguredService) ListAutomationRunsForScheduleOwner(context.Context, string, string, string, int) ([]manage.AutomationRunView, error) {
	return nil, serviceNotConfiguredError()
}

func serviceNotConfiguredError() error {
	return api.NewHTTPError(http.StatusNotImplemented, serviceNotConfiguredMessage)
}

func scheduleID(ctx *gin.Context) string {
	if ctx == nil {
		return ""
	}
	return strings.TrimSpace(ctx.Param("scheduleId"))
}
