package handler

import (
	"context"

	"git.optclblast.xyz/draincloud/draincloud-core/internal/common"
)

type WriteOptions struct {
	Code int
}

type WriteOption func(opts *WriteOptions)

func WithCode(code int) WriteOption {
	return func(opts *WriteOptions) {
		opts.Code = code
	}
}

type Writer interface {
	Write(ctx context.Context, resp any, opts ...WriteOption)
	SetCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool)
}

type Handler interface {
	GetName() string
	GetRequiredResolveParams() []string
	GetProcessFn() func(ctx context.Context, req *common.Request, w Writer) error
	GetPreprocessFn() func(ctx context.Context, req *common.Request, w Writer) error
}

type BaseHandler struct {
	Name                  string
	RequiredResolveParams []string
	ProcessFn             func(ctx context.Context, req *common.Request, w Writer) error
	PreprocessFn          func(ctx context.Context, req *common.Request, w Writer) error
}

func New() *BaseHandler {
	return new(BaseHandler)
}

func (h *BaseHandler) WithName(name string) *BaseHandler {
	h.Name = name
	return h
}

func (h *BaseHandler) WithRequiredResolveParams(params ...string) *BaseHandler {
	h.RequiredResolveParams = params
	return h
}

func (h *BaseHandler) WithProcessFunc(fn func(ctx context.Context, req *common.Request, w Writer) error) *BaseHandler {
	h.ProcessFn = fn
	return h
}

func (h *BaseHandler) WithPreprocessFunc(fn func(ctx context.Context, req *common.Request, w Writer) error) *BaseHandler {
	h.PreprocessFn = fn
	return h
}

func (h *BaseHandler) GetName() string {
	return h.Name
}

func (h *BaseHandler) GetRequiredResolveParams() []string {
	return h.RequiredResolveParams
}

func (h *BaseHandler) GetProcessFn() func(ctx context.Context, req *common.Request, w Writer) error {
	return h.ProcessFn
}

func (h *BaseHandler) GetPreprocessFn() func(ctx context.Context, req *common.Request, w Writer) error {
	return h.PreprocessFn
}
