// Copyright The kweaver.ai Authors.
//
// Licensed under the Apache License, Version 2.0.
// See the LICENSE file in the project root for details.

package filter_condition

import (
	"context"
	"fmt"

	"vega-backend/interfaces"
)

type NotLikeCond struct {
	Cfg    *interfaces.FilterCondCfg
	Lfield *interfaces.Property
	Value  string
}

func (c *NotLikeCond) GetOperation() string { return OperationNotLike }

func (c *NotLikeCond) SupportSubCond() bool       { return false }
func (c *NotLikeCond) NeedName() bool             { return true }
func (c *NotLikeCond) NeedValue() bool            { return true }
func (c *NotLikeCond) NeedConstValue() bool       { return true }
func (c *NotLikeCond) IsSingleValue() bool        { return true }
func (c *NotLikeCond) IsFixedLenArrayValue() bool { return false }
func (c *NotLikeCond) RequiredValueLen() int      { return -1 }

func (c *NotLikeCond) New(ctx context.Context, cfg *interfaces.FilterCondCfg,
	fieldsMap map[string]*interfaces.Property) (interfaces.FilterCondition, error) {

	if cfg.Name == "" {
		return nil, fmt.Errorf("condition [not_like] left field is empty")
	}
	field, ok := fieldsMap[cfg.Name]
	if !ok {
		return nil, fmt.Errorf("condition [not_like] left field '%s' not found", cfg.Name)
	}
	if interfaces.DataType_IsNumber(field.Type) {
		return nil, fmt.Errorf("condition [not_like] left field is not a string/text field: %s:%s", cfg.Name, field.Type)
	}

	if cfg.ValueFrom != interfaces.ValueFrom_Const {
		return nil, fmt.Errorf("condition [not_like] does not support value_from type '%s'", cfg.ValueFrom)
	}
	val, ok := cfg.Value.(string)
	if !ok {
		return nil, fmt.Errorf("condition [not_like] right value is not a string value: %v", cfg.Value)
	}

	return &NotLikeCond{
		Cfg:    cfg,
		Lfield: field,
		Value:  val,
	}, nil
}
