package label import ( "context" "fmt" "queryorchestration/internal/database/repository" "queryorchestration/internal/serviceconfig" "github.com/google/uuid" ) // Service provides label management operations type Service struct { cfg serviceconfig.ConfigProvider } // New creates a new label service instance func New(cfg serviceconfig.ConfigProvider) *Service { return &Service{ cfg: cfg, } } // ApplyLabel applies a label to a document with timestamp tracking // Can be called multiple times for the same document/label to track re-application history func (s *Service) ApplyLabel(ctx context.Context, documentID uuid.UUID, label string, appliedBy string) (*repository.Documentlabel, error) { if label == "" { return nil, fmt.Errorf("label cannot be empty") } if appliedBy == "" { return nil, fmt.Errorf("appliedBy cannot be empty") } result, err := s.cfg.GetDBQueries().ApplyLabel(ctx, &repository.ApplyLabelParams{ Documentid: documentID, Label: label, Appliedby: appliedBy, }) if err != nil { return nil, fmt.Errorf("failed to apply label: %w", err) } return result, nil } // GetDocumentLabels retrieves all label applications for a document // Returns labels ordered by most recent application first func (s *Service) GetDocumentLabels(ctx context.Context, documentID uuid.UUID) ([]*repository.Documentlabel, error) { labels, err := s.cfg.GetDBQueries().GetDocumentLabels(ctx, documentID) if err != nil { return nil, fmt.Errorf("failed to get document labels: %w", err) } return labels, nil } // GetMostRecentLabel retrieves the most recent application of a specific label for a document func (s *Service) GetMostRecentLabel(ctx context.Context, documentID uuid.UUID, label string) (*repository.Documentlabel, error) { result, err := s.cfg.GetDBQueries().GetMostRecentLabel(ctx, &repository.GetMostRecentLabelParams{ Documentid: documentID, Label: label, }) if err != nil { return nil, fmt.Errorf("label not found: %w", err) } return result, nil } // GetDocumentsByLabel retrieves all documents with a specific label for a client func (s *Service) GetDocumentsByLabel(ctx context.Context, clientID string, label string) ([]*repository.Document, error) { documents, err := s.cfg.GetDBQueries().GetDocumentsByLabel(ctx, &repository.GetDocumentsByLabelParams{ Clientid: clientID, Label: label, }) if err != nil { return nil, fmt.Errorf("failed to get documents by label: %w", err) } return documents, nil } // GetDocumentsByLabelAndFolder retrieves all documents with a specific label in a specific folder func (s *Service) GetDocumentsByLabelAndFolder(ctx context.Context, folderID uuid.UUID, label string) ([]*repository.Document, error) { documents, err := s.cfg.GetDBQueries().GetDocumentsByLabelAndFolder(ctx, &repository.GetDocumentsByLabelAndFolderParams{ Folderid: &folderID, Label: label, }) if err != nil { return nil, fmt.Errorf("failed to get documents by label and folder: %w", err) } return documents, nil } // GetAllLabels retrieves all available labels from the lookup table func (s *Service) GetAllLabels(ctx context.Context) ([]*repository.Label, error) { labels, err := s.cfg.GetDBQueries().GetAllLabels(ctx) if err != nil { return nil, fmt.Errorf("failed to get all labels: %w", err) } return labels, nil } // CreateLabel creates a new label in the lookup table func (s *Service) CreateLabel(ctx context.Context, label string, description string) (*repository.Label, error) { if label == "" { return nil, fmt.Errorf("label cannot be empty") } if description == "" { return nil, fmt.Errorf("description cannot be empty") } result, err := s.cfg.GetDBQueries().CreateLabel(ctx, &repository.CreateLabelParams{ Label: label, Description: description, }) if err != nil { return nil, fmt.Errorf("failed to create label: %w", err) } return result, nil } // GetDocumentLabelHistory retrieves label application history for a client with pagination func (s *Service) GetDocumentLabelHistory(ctx context.Context, clientID string, limit int32, offset int32) ([]*repository.GetDocumentLabelHistoryRow, error) { if limit <= 0 { return nil, fmt.Errorf("limit must be greater than 0") } if offset < 0 { return nil, fmt.Errorf("offset cannot be negative") } history, err := s.cfg.GetDBQueries().GetDocumentLabelHistory(ctx, &repository.GetDocumentLabelHistoryParams{ Clientid: clientID, Limit: int64(limit), Offset: int64(offset), }) if err != nil { return nil, fmt.Errorf("failed to get label history: %w", err) } return history, nil }