package search

import (
	"context"
	"fmt"
	"tercul/internal/domain"
	"time"

	"github.com/weaviate/weaviate-go-client/v5/weaviate"
)

type WeaviateWrapper interface {
	IndexWork(ctx context.Context, work *domain.Work, content string) error
}

type weaviateWrapper struct {
	client *weaviate.Client
}

func NewWeaviateWrapper(client *weaviate.Client) WeaviateWrapper {
	return &weaviateWrapper{client: client}
}

func (w *weaviateWrapper) IndexWork(ctx context.Context, work *domain.Work, content string) error {
	properties := map[string]interface{}{
		"language":    work.Language,
		"title":       work.Title,
		"description": work.Description,
		"status":      work.Status,
		"createdAt":   work.CreatedAt.Format(time.RFC3339),
		"updatedAt":   work.UpdatedAt.Format(time.RFC3339),
	}
	if content != "" {
		properties["content"] = content
	}

	_, err := w.client.Data().Creator().
		WithClassName("Work").
		WithID(fmt.Sprintf("%d", work.ID)).
		WithProperties(properties).
		Do(ctx)

	return err
}
