package dbstore

import (
	"git.linuxforward.com/byop/byop-engine/dbmanager"
)

// Store provides a generic implementation of Repository
type Store[T dbmanager.Entity] struct {
	Db         dbmanager.DbManager[T]
	EntityType string
}

// NewStore creates a new generic store
func NewStore[T dbmanager.Entity](db dbmanager.DbManager[T], entityType string) *Store[T] {
	return &Store[T]{
		Db:         db,
		EntityType: entityType,
	}
}

// Create creates a new entity
func (gs *Store[T]) Create(entity T) error {
	return gs.Db.Create(gs.EntityType, entity)
}

// GetByID retrieves an entity by ID
func (gs *Store[T]) GetByID(id string) (T, error) {
	entity, err := gs.Db.GetByID(gs.EntityType, id)
	var zero T
	if err != nil {
		return zero, err
	}
	// No need for nil check and type assertion if Db.GetByID returns T directly
	return entity, nil
}

// Update updates an existing entity
func (gs *Store[T]) Update(entity T) error {
	return gs.Db.Update(gs.EntityType, entity)
}

// Delete deletes an entity
func (gs *Store[T]) Delete(id string) error {
	return gs.Db.Delete(gs.EntityType, id)
}

// List retrieves entities based on a filter
func (gs *Store[T]) List(filter map[string]interface{}) ([]T, error) {
	entities, err := gs.Db.List(gs.EntityType, filter)
	if err != nil {
		return nil, err
	}

	// No type assertions needed if Db.List returns []T directly
	return entities, nil
}
