package utils

import (
	"encoding/json"
	"time"

	"cloud.o-forge.io/core/oc-lib/dbs"
	"cloud.o-forge.io/core/oc-lib/tools"
	"github.com/go-playground/validator/v10"
	"github.com/google/uuid"
	"github.com/rs/zerolog"
)

// single instance of the validator used in every model Struct to validate the fields
var validate = validator.New(validator.WithRequiredStructEnabled())

type AccessMode int

const (
	Private AccessMode = iota
	Public
)

/*
* AbstractObject is a struct that represents the basic fields of an object
* it defines the object id and name
* every data in base root model should inherit from this struct (only exception is the ResourceModel)
 */
type AbstractObject struct {
	UUID          string     `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
	Name          string     `json:"name,omitempty" bson:"name,omitempty" validate:"required"`
	IsDraft       bool       `json:"is_draft" bson:"is_draft" default:"false"`
	CreatorID     string     `json:"creator_id,omitempty" bson:"creator_id,omitempty"`
	UserCreatorID string     `json:"user_creator_id,omitempty" bson:"user_creator_id,omitempty"`
	CreationDate  time.Time  `json:"creation_date,omitempty" bson:"creation_date,omitempty"`
	UpdateDate    time.Time  `json:"update_date,omitempty" bson:"update_date,omitempty"`
	UpdaterID     string     `json:"updater_id,omitempty" bson:"updater_id,omitempty"`
	UserUpdaterID string     `json:"user_updater_id,omitempty" bson:"user_updater_id,omitempty"`
	AccessMode    AccessMode `json:"access_mode" bson:"access_mode" default:"0"`
}

func (ri *AbstractObject) GetAccessor(request *tools.APIRequest) Accessor {
	return nil
}

func (r *AbstractObject) GenerateID() {
	if r.UUID == "" {
		r.UUID = uuid.New().String()
	}
}

func (r *AbstractObject) StoreDraftDefault() {
	r.IsDraft = false
}

func (r *AbstractObject) CanUpdate(set DBObject) (bool, DBObject) {
	return true, set
}

func (r *AbstractObject) CanDelete() bool {
	return true
}

func (r *AbstractObject) IsDrafted() bool {
	return r.IsDraft
}

// GetID implements ShallowDBObject.
func (ao AbstractObject) GetID() string {
	return ao.UUID
}

// GetName implements ShallowDBObject.
func (ao AbstractObject) GetName() string {
	return ao.Name
}

func (ao *AbstractObject) GetCreatorID() string {
	return ao.CreatorID
}

func (ao *AbstractObject) UpToDate(user string, peer string, create bool) {
	ao.UpdateDate = time.Now()
	ao.UpdaterID = peer
	ao.UserUpdaterID = user
	if create {
		ao.CreationDate = time.Now()
		ao.CreatorID = peer
		ao.UserCreatorID = user
	}
}

func (ao *AbstractObject) VerifyAuth(request *tools.APIRequest) bool {
	return ao.AccessMode == Public || (request != nil && ao.CreatorID == request.PeerID)
}

func (ao *AbstractObject) GetObjectFilters(search string) *dbs.Filters {
	if search == "*" {
		search = ""
	}
	return &dbs.Filters{
		Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
			"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
		}}
}

func (dma *AbstractObject) Deserialize(j map[string]interface{}, obj DBObject) DBObject {
	b, err := json.Marshal(j)
	if err != nil {
		return nil
	}
	json.Unmarshal(b, obj)
	return obj
}

func (dma *AbstractObject) Serialize(obj DBObject) map[string]interface{} {
	var m map[string]interface{}
	b, err := json.Marshal(obj)
	if err != nil {
		return nil
	}
	json.Unmarshal(b, &m)
	return m
}

type AbstractAccessor struct {
	Logger                zerolog.Logger    // Logger is the logger of the accessor, it's a specilized logger for the accessor
	Type                  tools.DataType    // Type is the data type of the accessor
	Request               *tools.APIRequest // Caller is the http caller of the accessor (optionnal) only need in a peer connection
	ResourceModelAccessor Accessor
}

func (r *AbstractAccessor) ShouldVerifyAuth() bool {
	return true
}

func (r *AbstractAccessor) GetRequest() *tools.APIRequest {
	return r.Request
}

func (dma *AbstractAccessor) GetUser() string {
	if dma.Request == nil {
		return ""
	}
	return dma.Request.Username
}

func (dma *AbstractAccessor) GetPeerID() string {
	if dma.Request == nil {
		return ""
	}
	return dma.Request.PeerID
}
func (dma *AbstractAccessor) GetGroups() []string {
	if dma.Request == nil {
		return []string{}
	}
	return dma.Request.Groups
}
func (dma *AbstractAccessor) GetLogger() *zerolog.Logger {
	return &dma.Logger
}
func (dma *AbstractAccessor) GetType() tools.DataType {
	return dma.Type
}

func (dma *AbstractAccessor) GetCaller() *tools.HTTPCaller {
	if dma.Request == nil {
		return nil
	}
	return dma.Request.Caller
}
