package crud

import (
	"context"
	"git.nspix.com/golang/micro/gateway/http"
	"gorm.io/gorm"
)

type Callbacks struct {
	BeforeQuery  QueryCallback
	EachQuery    EachQueryCallback
	BeforeSave   SaveCallback
	AfterSave    SaveCallback
	BeforeInsert SaveCallback
	AfterInsert  SaveCallback
	BeforeUpdate SaveCallback
	AfterUpdate  SaveCallback
	BeforeDelete DeleteCallback
	AfterDelete  SaveCallback
}

type Options struct {
	ctx        context.Context
	enable     *int32
	DB         *gorm.DB
	Formatter  *Formatter
	Prefix     string
	Callbacks  Callbacks
	Middleware []http.Middleware
	Scenarios  []string
}

type Option func(o *Options)

func WithContext(ctx context.Context) Option {
	return func(o *Options) {
		o.ctx = ctx
	}
}

func WithMiddleware(middleware ...http.Middleware) Option {
	return func(o *Options) {
		o.Middleware = middleware
	}
}

func WithFormatter(formatter *Formatter) Option {
	return func(o *Options) {
		o.Formatter = formatter
	}
}

func WithScenarios(ss []string) Option {
	return func(o *Options) {
		o.Scenarios = ss
	}
}

func WithDB(db *gorm.DB) Option {
	return func(o *Options) {
		o.DB = db
	}
}

func WithPrefix(prefix string) Option {
	return func(o *Options) {
		o.Prefix = prefix
	}
}

func WithEnable(enable *int32) Option {
	return func(o *Options) {
		o.enable = enable
	}
}

func WithCallback(cbs Callbacks) Option {
	return func(o *Options) {
		o.Callbacks = cbs
	}
}
