// Generated by [optioner] command-line tool; DO NOT EDIT
// If you have any questions, please create issues and submit contributions at:
// https://github.com/chenmingyong0423/go-optioner

// Copyright 2024 chenmingyong0423

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package operation

import (
	"reflect"
	"time"

	"github.com/chenmingyong0423/go-mongox/v2/field"

	"go.mongodb.org/mongo-driver/v2/mongo"
)

type OpType string

const (
	OpTypeBeforeInsert    OpType = "beforeInsert"
	OpTypeAfterInsert     OpType = "afterInsert"
	OpTypeBeforeUpdate    OpType = "beforeUpdate"
	OpTypeAfterUpdate     OpType = "afterUpdate"
	OpTypeBeforeDelete    OpType = "beforeDelete"
	OpTypeAfterDelete     OpType = "afterDelete"
	OpTypeBeforeUpsert    OpType = "beforeUpsert"
	OpTypeAfterUpsert     OpType = "afterUpsert"
	OpTypeBeforeFind      OpType = "beforeFind"
	OpTypeAfterFind       OpType = "afterFind"
	OpTypeBeforeAggregate OpType = "beforeAggregate"
	OpTypeAfterAggregate  OpType = "afterAggregate"
	OpTypeBeforeAny       OpType = "before*"
	OpTypeAfterAny        OpType = "after*"
)

//go:generate optioner -type OpContext -output operation_type.go -mode append
type OpContext struct {
	Col    *mongo.Collection `opt:"-"`
	Fields []*field.Filed

	Doc any
	// filter also can be used as query
	Filter       any
	Updates      any
	Pipeline     any
	MongoOptions any
	ModelHook    any
	ReflectValue reflect.Value
	StartTime    time.Time

	// result of the collection operation
	Result any
}

type OpContextOption func(*OpContext)

func NewOpContext(col *mongo.Collection, opts ...OpContextOption) *OpContext {
	opContext := &OpContext{
		Col: col,
	}

	for _, opt := range opts {
		opt(opContext)
	}

	return opContext
}

func WithFields(fields []*field.Filed) OpContextOption {
	return func(opContext *OpContext) {
		opContext.Fields = fields
	}
}

func WithDoc(doc any) OpContextOption {
	return func(opContext *OpContext) {
		opContext.Doc = doc
	}
}

func WithFilter(filter any) OpContextOption {
	return func(opContext *OpContext) {
		opContext.Filter = filter
	}
}

func WithUpdates(updates any) OpContextOption {
	return func(opContext *OpContext) {
		opContext.Updates = updates
	}
}

func WithPipeline(pipeline any) OpContextOption {
	return func(opContext *OpContext) {
		opContext.Pipeline = pipeline
	}
}

func WithMongoOptions(mongoOptions any) OpContextOption {
	return func(opContext *OpContext) {
		opContext.MongoOptions = mongoOptions
	}
}

func WithModelHook(modelHook any) OpContextOption {
	return func(opContext *OpContext) {
		opContext.ModelHook = modelHook
	}
}

func WithReflectValue(reflectValue reflect.Value) OpContextOption {
	return func(opContext *OpContext) {
		opContext.ReflectValue = reflectValue
	}
}

func WithStartTime(startTime time.Time) OpContextOption {
	return func(opContext *OpContext) {
		opContext.StartTime = startTime
	}
}

func WithResult(result any) OpContextOption {
	return func(opContext *OpContext) {
		opContext.Result = result
	}
}
