package minio

import "time"

// Config holds MinIO connection configuration
type Config struct {
	Endpoint        string        `json:"endpoint" yaml:"endpoint"`
	AccessKeyID     string        `json:"accessKeyID" yaml:"accessKeyID"`
	SecretAccessKey string        `json:"secretAccessKey" yaml:"secretAccessKey"`
	UseSSL          bool          `json:"useSSL" yaml:"useSSL"`
	Region          string        `json:"region" yaml:"region"`

	// Connection settings
	ConnectTimeout time.Duration `json:"connectTimeout" yaml:"connectTimeout"`
	RequestTimeout time.Duration `json:"requestTimeout" yaml:"requestTimeout"`

	// Bucket settings
	DefaultBucket string `json:"defaultBucket" yaml:"defaultBucket"`

	// Hierarchical storage settings
	HierarchicalBucket string `json:"hierarchicalBucket" yaml:"hierarchicalBucket"`
	PathSeparator      string `json:"pathSeparator" yaml:"pathSeparator"`

	// Upload settings
	MaxFileSize     int64 `json:"maxFileSize" yaml:"maxFileSize"`         // Maximum file size in bytes
	AllowedTypes    []string `json:"allowedTypes" yaml:"allowedTypes"`   // Allowed MIME types
	ChunkSize       int64 `json:"chunkSize" yaml:"chunkSize"`            // Chunk size for multipart uploads
}

// DefaultConfig returns a default MinIO configuration
func DefaultConfig() Config {
	return Config{
		UseSSL:            true,
		Region:            "us-east-1",
		ConnectTimeout:    10 * time.Second,
		RequestTimeout:    30 * time.Second,
		DefaultBucket:     "files",
		HierarchicalBucket: "documents",
		PathSeparator:     "/",
		MaxFileSize:       100 * 1024 * 1024, // 100MB
		AllowedTypes:      []string{"*"},     // Allow all types by default
		ChunkSize:         5 * 1024 * 1024,   // 5MB chunks
	}
}

// Validate validates the MinIO configuration
func (c *Config) Validate() error {
	if c.Endpoint == "" {
		return NewErrorWithCause(ErrCodeInvalidConfig, "endpoint is required", ErrInvalidConfig)
	}
	if c.AccessKeyID == "" {
		return NewErrorWithCause(ErrCodeInvalidConfig, "accessKeyID is required", ErrInvalidConfig)
	}
	if c.SecretAccessKey == "" {
		return NewErrorWithCause(ErrCodeInvalidConfig, "secretAccessKey is required", ErrInvalidConfig)
	}
	if c.DefaultBucket == "" {
		return NewErrorWithCause(ErrCodeInvalidConfig, "defaultBucket is required", ErrInvalidConfig)
	}
	if c.HierarchicalBucket == "" {
		return NewErrorWithCause(ErrCodeInvalidConfig, "hierarchicalBucket is required", ErrInvalidConfig)
	}
	if c.PathSeparator == "" {
		c.PathSeparator = "/"
	}
	if c.MaxFileSize <= 0 {
		c.MaxFileSize = 100 * 1024 * 1024 // 100MB default
	}
	if len(c.AllowedTypes) == 0 {
		c.AllowedTypes = []string{"*"}
	}
	if c.ChunkSize <= 0 {
		c.ChunkSize = 5 * 1024 * 1024 // 5MB default
	}
	return nil
}
