package papitemplate

import (
	"github.com/tech/sendico/server/interface/api/sresponse"
)

type HandlerResolver func(sresponse.AccountHandlerFunc) sresponse.AccountHandlerFunc

type Config interface {
	WithNoCreate() Config
	WithCreateHandler(handler sresponse.AccountHandlerFunc) Config
	WithNoList() Config
	WithListHandler(handler sresponse.AccountHandlerFunc) Config
	WithNoGet() Config
	WithGetHandler(handler sresponse.AccountHandlerFunc) Config
	WithNoUpdate() Config
	WithUpdateHandler(handler sresponse.AccountHandlerFunc) Config
	WithNoDelete() Config
	WithDeleteHandler(handler sresponse.AccountHandlerFunc) Config
	WithReorderHandler(reorder ReorderConfig) Config
	WithTaggableHandler(taggable TaggableConfig) Config
}

type PAPIConfig struct {
	CreateResolver  HandlerResolver
	ListResolver    HandlerResolver
	GetResolver     HandlerResolver
	UpdateResolver  HandlerResolver
	DeleteResolver  HandlerResolver
	ArchiveResolver HandlerResolver
	Reorder         *ReorderConfig
	Taggable        *TaggableConfig
}

// WithNoCreate disables the create endpoint by replacing its resolver.
func (cfg *PAPIConfig) WithNoCreate() *PAPIConfig {
	cfg.CreateResolver = disableResolver
	return cfg
}

// WithCreateHandler overrides the create endpoint by replacing its resolver.
func (cfg *PAPIConfig) WithCreateHandler(handler sresponse.AccountHandlerFunc) *PAPIConfig {
	cfg.CreateResolver = overrideResolver(handler)
	return cfg
}

// WithNoList disables the list endpoint.
func (cfg *PAPIConfig) WithNoList() *PAPIConfig {
	cfg.ListResolver = disableResolver
	return cfg
}

// WithListHandler overrides the list endpoint.
func (cfg *PAPIConfig) WithListHandler(handler sresponse.AccountHandlerFunc) *PAPIConfig {
	cfg.ListResolver = overrideResolver(handler)
	return cfg
}

// WithNoGet disables the get endpoint.
func (cfg *PAPIConfig) WithNoGet() *PAPIConfig {
	cfg.GetResolver = disableResolver
	return cfg
}

// WithGetHandler overrides the get endpoint.
func (cfg *PAPIConfig) WithGetHandler(handler sresponse.AccountHandlerFunc) *PAPIConfig {
	cfg.GetResolver = overrideResolver(handler)
	return cfg
}

// WithNoUpdate disables the update endpoint.
func (cfg *PAPIConfig) WithNoUpdate() *PAPIConfig {
	cfg.UpdateResolver = disableResolver
	return cfg
}

// WithUpdateHandler overrides the update endpoint.
func (cfg *PAPIConfig) WithUpdateHandler(handler sresponse.AccountHandlerFunc) *PAPIConfig {
	cfg.UpdateResolver = overrideResolver(handler)
	return cfg
}

// WithNoDelete disables the delete endpoint.
func (cfg *PAPIConfig) WithNoDelete() *PAPIConfig {
	cfg.DeleteResolver = disableResolver
	return cfg
}

// WithDeleteHandler overrides the delete endpoint.
func (cfg *PAPIConfig) WithDeleteHandler(handler sresponse.AccountHandlerFunc) *PAPIConfig {
	cfg.DeleteResolver = overrideResolver(handler)
	return cfg
}

func (cfg *PAPIConfig) WithNoArchive() *PAPIConfig {
	cfg.ArchiveResolver = disableResolver
	return cfg
}

func (cfg *PAPIConfig) WithArchiveHandler(handler sresponse.AccountHandlerFunc) *PAPIConfig {
	cfg.ArchiveResolver = overrideResolver(handler)
	return cfg
}

// defaultResolver returns the default handler unchanged.
func defaultResolver(defaultHandler sresponse.AccountHandlerFunc) sresponse.AccountHandlerFunc {
	return defaultHandler
}

// disableResolver always returns nil, disabling the endpoint.
func disableResolver(_ sresponse.AccountHandlerFunc) sresponse.AccountHandlerFunc {
	return nil
}

// overrideResolver returns a resolver that always returns the given custom handler.
func overrideResolver(custom sresponse.AccountHandlerFunc) HandlerResolver {
	return func(_ sresponse.AccountHandlerFunc) sresponse.AccountHandlerFunc {
		return custom
	}
}

func NewConfig() *PAPIConfig {
	return &PAPIConfig{
		CreateResolver:  defaultResolver,
		ListResolver:    defaultResolver,
		GetResolver:     defaultResolver,
		UpdateResolver:  defaultResolver,
		DeleteResolver:  defaultResolver,
		ArchiveResolver: defaultResolver,
		Reorder:         nil,
		Taggable:        nil,
	}
}
