package whatshooked

import "git.warky.dev/wdevs/whatshooked/pkg/config"

// Option is a functional option for configuring WhatsHooked
type Option func(*config.Config)

// WithServer configures server settings
func WithServer(host string, port int) Option {
	return func(c *config.Config) {
		c.Server.Host = host
		c.Server.Port = port
	}
}

// WithAuth configures authentication
func WithAuth(apiKey, username, password string) Option {
	return func(c *config.Config) {
		c.Server.AuthKey = apiKey
		c.Server.Username = username
		c.Server.Password = password
	}
}

// WithDefaultCountryCode sets the default country code for phone numbers
func WithDefaultCountryCode(code string) Option {
	return func(c *config.Config) {
		c.Server.DefaultCountryCode = code
	}
}

// WithWhatsAppAccount adds a WhatsApp account
func WithWhatsAppAccount(account config.WhatsAppConfig) Option {
	return func(c *config.Config) {
		c.WhatsApp = append(c.WhatsApp, account)
	}
}

// WithWhatsmeowAccount adds a Whatsmeow account (convenience)
func WithWhatsmeowAccount(id, phoneNumber, sessionPath string, showQR bool) Option {
	return func(c *config.Config) {
		c.WhatsApp = append(c.WhatsApp, config.WhatsAppConfig{
			ID:          id,
			Type:        "whatsmeow",
			PhoneNumber: phoneNumber,
			SessionPath: sessionPath,
			ShowQR:      showQR,
		})
	}
}

// WithBusinessAPIAccount adds a Business API account (convenience)
func WithBusinessAPIAccount(id, phoneNumber, phoneNumberID, accessToken, verifyToken string) Option {
	return func(c *config.Config) {
		c.WhatsApp = append(c.WhatsApp, config.WhatsAppConfig{
			ID:          id,
			Type:        "business-api",
			PhoneNumber: phoneNumber,
			BusinessAPI: &config.BusinessAPIConfig{
				PhoneNumberID: phoneNumberID,
				AccessToken:   accessToken,
				VerifyToken:   verifyToken,
				APIVersion:    "v21.0",
			},
		})
	}
}

// WithHook adds a webhook
func WithHook(hook config.Hook) Option {
	return func(c *config.Config) {
		c.Hooks = append(c.Hooks, hook)
	}
}

// WithEventLogger enables event logging
func WithEventLogger(targets []string, fileDir string) Option {
	return func(c *config.Config) {
		c.EventLogger.Enabled = true
		c.EventLogger.Targets = targets
		c.EventLogger.FileDir = fileDir
	}
}

// WithEventLoggerConfig configures event logger with full config
func WithEventLoggerConfig(cfg config.EventLoggerConfig) Option {
	return func(c *config.Config) {
		c.EventLogger = cfg
	}
}

// WithMedia configures media settings
func WithMedia(dataPath, mode, baseURL string) Option {
	return func(c *config.Config) {
		c.Media.DataPath = dataPath
		c.Media.Mode = mode
		c.Media.BaseURL = baseURL
	}
}

// WithMediaConfig configures media with full config
func WithMediaConfig(cfg config.MediaConfig) Option {
	return func(c *config.Config) {
		c.Media = cfg
	}
}

// WithLogLevel sets the log level
func WithLogLevel(level string) Option {
	return func(c *config.Config) {
		c.LogLevel = level
	}
}

// WithDatabase configures database settings
func WithDatabase(dbType, host string, port int, username, password, database string) Option {
	return func(c *config.Config) {
		c.Database.Type = dbType
		c.Database.Host = host
		c.Database.Port = port
		c.Database.Username = username
		c.Database.Password = password
		c.Database.Database = database
	}
}

// WithSQLiteDatabase configures SQLite database
func WithSQLiteDatabase(sqlitePath string) Option {
	return func(c *config.Config) {
		c.Database.Type = "sqlite"
		c.Database.SQLitePath = sqlitePath
	}
}

// WithTLS configures TLS settings
func WithTLS(enabled bool, mode string) Option {
	return func(c *config.Config) {
		c.Server.TLS.Enabled = enabled
		c.Server.TLS.Mode = mode
	}
}

// WithTLSConfig configures TLS with full config
func WithTLSConfig(cfg config.TLSConfig) Option {
	return func(c *config.Config) {
		c.Server.TLS = cfg
	}
}

// WithSelfSignedTLS enables HTTPS with self-signed certificates
func WithSelfSignedTLS(certDir string) Option {
	return func(c *config.Config) {
		c.Server.TLS.Enabled = true
		c.Server.TLS.Mode = "self-signed"
		c.Server.TLS.CertDir = certDir
	}
}

// WithCustomTLS enables HTTPS with custom certificate files
func WithCustomTLS(certFile, keyFile string) Option {
	return func(c *config.Config) {
		c.Server.TLS.Enabled = true
		c.Server.TLS.Mode = "custom"
		c.Server.TLS.CertFile = certFile
		c.Server.TLS.KeyFile = keyFile
	}
}

// WithAutocertTLS enables HTTPS with Let's Encrypt autocert
func WithAutocertTLS(domain, email string, production bool) Option {
	return func(c *config.Config) {
		c.Server.TLS.Enabled = true
		c.Server.TLS.Mode = "autocert"
		c.Server.TLS.Domain = domain
		c.Server.TLS.Email = email
		c.Server.TLS.Production = production
	}
}
