package calendar

import (
	"errors"

	"github.com/tidbcloud/AgentTeam/internal/store/storeerr"
)

type ErrorCode string

const (
	ErrorCodeInvalidWindow  ErrorCode = "workflow_calendar_invalid_window"
	ErrorCodeWindowTooDense ErrorCode = "workflow_calendar_window_too_dense"
)

type RequestError struct {
	Kind    error
	Code    ErrorCode
	Message string
}

func (err RequestError) Error() string { return err.Message }
func (err RequestError) Unwrap() error { return err.Kind }

// WorkflowPublicError implements publicerr.DomainError so the centralized
// Workflow error classifier can extract the stable calendar code without
// importing this domain package.
func (err RequestError) WorkflowPublicError() (string, any) {
	return string(err.Code), nil
}

func ErrorDetails(err error) (ErrorCode, bool) {
	var requestError RequestError
	if !errors.As(err, &requestError) {
		return "", false
	}
	return requestError.Code, true
}

func invalidWindow(message string) error {
	return RequestError{Kind: storeerr.ErrBadRequest, Code: ErrorCodeInvalidWindow, Message: message}
}

func windowTooDense(message string) error {
	return RequestError{Kind: storeerr.ErrUnprocessable, Code: ErrorCodeWindowTooDense, Message: message}
}
