package ai_summarizer

import (
	"errors"
	"fmt"
	"time"
)

// Config holds the configuration for the AI Summarizer service.
// It contains settings for both the HTTP API and the MinIO storage integration.
type Config struct {
	// HTTP API settings
	APIBaseURL    string        // Base URL of the AI service, e.g. "http://ai-host:8000"
	APITimeout    time.Duration // Timeout for HTTP requests to the AI API
	APIRetryCount int           // Number of retry attempts for failed API requests
	APIRetryDelay time.Duration // Delay between retry attempts

	// MinIO storage settings
	MinioEndpoint  string        // MinIO endpoint, e.g. "ai-host:9000"
	MinioAccessKey string        // MinIO access key ID
	MinioSecretKey string        // MinIO secret access key
	MinioUseSSL    bool          // Whether to use SSL for MinIO connection
	MinioRegion    string        // MinIO region
	MinioBucket    string        // MinIO bucket name (default: "opplens-documents")
	MinioTimeout   time.Duration // Timeout for MinIO operations
}

// DefaultConfig returns a Config with sensible defaults.
func DefaultConfig() *Config {
	return &Config{
		APITimeout:    120 * time.Second,
		APIRetryCount: 2,
		APIRetryDelay: 3 * time.Second,

		MinioBucket:  "opplens-documents",
		MinioRegion:  "us-east-1",
		MinioUseSSL:  false,
		MinioTimeout: 30 * time.Second,
	}
}

// Validate checks that all required configuration fields are present.
// Use this when both HTTP API and MinIO storage are needed.
func (c *Config) Validate() error {
	if c.APIBaseURL == "" {
		return errors.New("ai_summarizer: APIBaseURL is required")
	}
	return c.ValidateStorage()
}

// ValidateStorage checks only the MinIO storage-related configuration fields.
// Use this when only the storage client is needed (no HTTP API).
func (c *Config) ValidateStorage() error {
	if c.MinioEndpoint == "" {
		return errors.New("ai_summarizer: MinioEndpoint is required")
	}
	if c.MinioAccessKey == "" {
		return errors.New("ai_summarizer: MinioAccessKey is required")
	}
	if c.MinioSecretKey == "" {
		return errors.New("ai_summarizer: MinioSecretKey is required")
	}
	if c.MinioBucket == "" {
		return fmt.Errorf("ai_summarizer: MinioBucket is required")
	}
	return nil
}

// ValidateAPI checks only the HTTP API-related configuration fields.
// Use this when only the HTTP client is needed (no MinIO storage).
func (c *Config) ValidateAPI() error {
	if c.APIBaseURL == "" {
		return errors.New("ai_summarizer: APIBaseURL is required")
	}
	return nil
}
