// File Path: monorepo/cloud/maplepress-backend/pkg/search/meilisearch.go
package search

import (
	"fmt"

	"github.com/meilisearch/meilisearch-go"
	"go.uber.org/zap"
)

// Client wraps the Meilisearch client
type Client struct {
	client meilisearch.ServiceManager
	config *Config
	logger *zap.Logger
}

// NewClient creates a new Meilisearch client
func NewClient(config *Config, logger *zap.Logger) (*Client, error) {
	if config.Host == "" {
		return nil, fmt.Errorf("meilisearch host is required")
	}

	client := meilisearch.New(config.Host, meilisearch.WithAPIKey(config.APIKey))

	return &Client{
		client: client,
		config: config,
		logger: logger.Named("meilisearch"),
	}, nil
}

// GetIndexName returns the full index name for a site
func (c *Client) GetIndexName(siteID string) string {
	return c.config.IndexPrefix + siteID
}

// Health checks if Meilisearch is healthy
func (c *Client) Health() error {
	_, err := c.client.Health()
	return err
}

// GetClient returns the underlying Meilisearch client (for advanced operations)
func (c *Client) GetClient() meilisearch.ServiceManager {
	return c.client
}
