// Package config defines the typed configuration model for the API service.
package config

import "time"

// Config holds all runtime configuration for the service.
type Config struct {
	Server     ServerConfig
	Database   DatabaseConfig
	Redis      RedisConfig
	Cache      CacheConfig
	JWT        JWTConfig
	Admin      AdminConfig
	Storage    StorageConfig
	Security   SecurityConfig
	Plugins    PluginsConfig
	AIAgentURL string // base URL of the ai-agent service, e.g. http://ai-agent:8080
	Env        string // development | production
}

// ServerConfig holds HTTP server settings.
type ServerConfig struct {
	Port         string
	CookieSecure bool   // set Secure flag on auth cookies; enable when behind an SSL-terminating proxy
	PublicURL    string // externally reachable base URL (for example, https://paca.example.com)
}

// AdminConfig holds the default administrator credentials seeded on first startup.
type AdminConfig struct {
	Username string
	Password string
}

// DatabaseConfig holds the primary database connection settings.
type DatabaseConfig struct {
	DSN string
}

// RedisConfig holds Redis connection settings.
type RedisConfig struct {
	URL string
}

// CacheConfig holds TTL settings for the different cache categories.
//
// Each TTL controls how long the corresponding data is served from Valkey/Redis
// before a fresh database read is made.  Set a TTL to zero to disable caching
// for that category entirely.
//
// Environment variables (loaded by config.Load):
//
//	CACHE_PROJECT_TTL  – project + member data          (default: 5m)
//	CACHE_CONFIG_TTL   – task types, statuses, custom
//	                     field definitions, and roles    (default: 10m)
//	CACHE_SPRINT_TTL   – sprints and views               (default: 2m)
type CacheConfig struct {
	// ProjectTTL is the cache duration for project detail and member list data.
	ProjectTTL time.Duration
	// ConfigTTL is the cache duration for infrequently-changing project
	// configuration: task types, task statuses, custom field definitions, and
	// project roles.  Global roles also use this TTL.
	ConfigTTL time.Duration
	// SprintTTL is the cache duration for sprint and view configuration data.
	SprintTTL time.Duration
}

// JWTConfig holds JWT signing and expiry settings.
type JWTConfig struct {
	Secret            string
	AccessTTL         time.Duration
	RefreshTTL        time.Duration // persistent session (remember me = true)
	RefreshSessionTTL time.Duration // ephemeral session (remember me = false)
}

// StorageConfig holds object-storage settings.
// When Provider is "s3" the service connects to AWS S3 using the Region field.
// When Provider is "minio" (default) it targets the Endpoint URL.
// The bucket is created automatically on startup if it does not exist.
type StorageConfig struct {
	Provider        string // "s3" | "minio"  (default: "minio")
	Endpoint        string // MinIO URL, e.g. "minio:9000"; ignored for AWS S3
	PublicURL       string // public-facing base URL for presigned URLs, e.g. "http://localhost/storage"
	Region          string // AWS region; used for S3; also supplied to MinIO (can be any value)
	Bucket          string
	AccessKeyID     string
	SecretAccessKey string
	UseSSL          bool // set true when Endpoint is HTTPS
}

// PluginsConfig holds runtime settings for the plugin subsystem.
type PluginsConfig struct {
	// Store selects where WASM plugin binaries are loaded from.
	// Accepted values: "local" (default) or "s3".
	// When "local", WASMDir is the root directory on the local filesystem.
	// When "s3", the Storage bucket and prefix are reused (STORAGE_BUCKET /
	// PLUGINS_S3_PREFIX).
	Store string

	// WASMDir is the local filesystem directory that contains plugin WASM
	// binaries.  Each plugin is expected at {WASMDir}/{pluginName}/backend.wasm.
	// Only used when Store is "local".  Defaults to "./plugins/local/backend".
	WASMDir string

	// FrontendDir is the local filesystem directory that contains extracted
	// frontend assets for installed plugins.
	// Each plugin is expected at {FrontendDir}/{pluginName}/assets/remoteEntry.js.
	FrontendDir string

	// MCPDir is the local filesystem directory that contains extracted MCP
	// bundles for installed plugins.  Served at /plugins-mcp/<pluginName>/.
	// Each plugin is expected at {MCPDir}/{pluginName}/mcp.js.
	MCPDir string

	// S3Prefix is the S3 key prefix used when Store is "s3".
	// Plugin WASM binaries are fetched from {S3Prefix}/{pluginName}/backend.wasm.
	S3Prefix string

	// MarketplaceCatalogURL points to a public JSON catalog in a GitHub repository
	// (for example, the raw URL of paca-plugins/catalog/plugins.json).
	MarketplaceCatalogURL string

	// MarketplaceTimeout is the HTTP timeout used when fetching marketplace
	// metadata and artifacts.
	MarketplaceTimeout time.Duration
}

// SecurityConfig holds secrets used by first-party and plugin features.
type SecurityConfig struct {
	// EncryptionKey is a 32-byte AES-256 key (hex-encoded) used to encrypt
	// sensitive data at rest.
	EncryptionKey string

	// AgentAPIKey is a pre-shared secret that the AI agent service uses to
	// authenticate against the Paca API.  When set, the API accepts this key
	// via the X-API-Key header and authenticates the request as the built-in
	// agent bot user — no database lookup is required.
	// Configure via the AGENT_API_KEY environment variable.
	AgentAPIKey string
}
