package purchase_resource

import (
	"time"

	"cloud.o-forge.io/core/oc-lib/logs"
	"cloud.o-forge.io/core/oc-lib/models/utils"
	"cloud.o-forge.io/core/oc-lib/tools"
)

type PurchaseResourceMongoAccessor struct {
	utils.AbstractAccessor[*PurchaseResource] // AbstractAccessor contains the basic fields of an accessor (model, caller)
}

// New creates a new instance of the bookingMongoAccessor
func NewAccessor(request *tools.APIRequest) *PurchaseResourceMongoAccessor {
	return &PurchaseResourceMongoAccessor{
		AbstractAccessor: utils.AbstractAccessor[*PurchaseResource]{
			Logger:  logs.CreateLogger(tools.PURCHASE_RESOURCE.String()), // Create a logger with the data type
			Request: request,
			Type:    tools.PURCHASE_RESOURCE,
			New:     func() *PurchaseResource { return &PurchaseResource{} },
		},
	}
}

/*
* Nothing special here, just the basic CRUD operations
 */
func (a *PurchaseResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
	return utils.GenericLoadOne(id, a.New(), func(d utils.DBObject) (utils.DBObject, int, error) {
		if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
			utils.GenericDelete(d, a)
			return nil, 404, nil
		}
		return d, 200, nil
	}, a)
}

func (a *PurchaseResourceMongoAccessor) GetExec(isDraft bool) func(utils.DBObject) utils.ShallowDBObject {
	return func(d utils.DBObject) utils.ShallowDBObject {
		if d.(*PurchaseResource).EndDate != nil && time.Now().UTC().After(*d.(*PurchaseResource).EndDate) {
			utils.GenericDelete(d, a)
			return nil
		}
		return d
	}
}

func (dca *PurchaseResourceMongoAccessor) ShouldVerifyAuth() bool {
	return false // TEMP : by pass
}
