package sync

import (
	"context"
	"log"
	"tercul/internal/platform/config"

	"github.com/hibiken/asynq"
	"github.com/weaviate/weaviate-go-client/v5/weaviate"
	"gorm.io/gorm"
)

// SyncJob manages the sync process.
type SyncJob struct {
	DB             *gorm.DB
	AsynqClient    *asynq.Client
	Cfg            *config.Config
	WeaviateClient *weaviate.Client
}

// NewSyncJob initializes a new SyncJob.
func NewSyncJob(db *gorm.DB, aClient *asynq.Client, cfg *config.Config, weaviateClient *weaviate.Client) *SyncJob {
	return &SyncJob{
		DB:             db,
		AsynqClient:    aClient,
		Cfg:            cfg,
		WeaviateClient: weaviateClient,
	}
}

// RunFullSync performs a full sync of all entities and edges.
func (s *SyncJob) RunFullSync(ctx context.Context) error {
	log.Println("Starting full database sync...")

	if err := s.SyncAllEntities(ctx); err != nil {
		return err
	}

	if err := s.SyncAllEdges(ctx); err != nil {
		return err
	}

	log.Println("Database sync completed successfully.")
	return nil
}
