1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
 2//
 3// SPDX-License-Identifier: AGPL-3.0-or-later
 4
 5package config
 6
 7import (
 8	"fmt"
 9)
10
11// Config represents the server configuration
12type Config struct {
13	// Server settings
14	Server ServerConfig `mapstructure:"server" toml:"server"`
15
16	// Logging configuration
17	Logging LoggingConfig `mapstructure:"logging" toml:"logging"`
18
19	// Planning limits
20	Planning PlanningConfig `mapstructure:"planning" toml:"planning"`
21}
22
23// ServerConfig contains server-related settings
24type ServerConfig struct {
25	Mode string `mapstructure:"mode" toml:"mode"`
26	Host string `mapstructure:"host" toml:"host"`
27	Port int    `mapstructure:"port" toml:"port"`
28}
29
30// LoggingConfig contains logging settings
31type LoggingConfig struct {
32	Level  string `mapstructure:"level" toml:"level"`
33	Format string `mapstructure:"format" toml:"format"`
34}
35
36// PlanningConfig contains planning-related limits
37type PlanningConfig struct {
38	MaxTasks      int `mapstructure:"max_tasks" toml:"max_tasks"`
39	MaxGoalLength int `mapstructure:"max_goal_length" toml:"max_goal_length"`
40	MaxTaskLength int `mapstructure:"max_task_length" toml:"max_task_length"`
41	// Reserved for future persistence/history features
42	HistoryEnabled bool `mapstructure:"history_enabled" toml:"history_enabled"`
43}
44
45// Default returns a Config with sensible defaults
46func Default() *Config {
47	return &Config{
48		Server: ServerConfig{
49			Mode: "stdio",
50			Host: "localhost",
51			Port: 8080,
52		},
53		Logging: LoggingConfig{
54			Level:  "info",
55			Format: "text",
56		},
57		Planning: PlanningConfig{
58			MaxTasks:       100,
59			MaxGoalLength:  1000,
60			MaxTaskLength:  500,
61			HistoryEnabled: true,
62		},
63	}
64}
65
66// Validate validates the configuration
67func (c *Config) Validate() error {
68	if c.Server.Mode != "stdio" && c.Server.Mode != "http" {
69		return fmt.Errorf("server mode must be 'stdio' or 'http'")
70	}
71
72	if c.Server.Mode == "http" && (c.Server.Port <= 0 || c.Server.Port > 65535) {
73		return fmt.Errorf("server port must be between 1 and 65535")
74	}
75
76	if c.Planning.MaxTasks <= 0 {
77		return fmt.Errorf("max tasks must be positive")
78	}
79
80	if c.Planning.MaxGoalLength <= 0 {
81		return fmt.Errorf("max goal length must be positive")
82	}
83
84	if c.Planning.MaxTaskLength <= 0 {
85		return fmt.Errorf("max task length must be positive")
86	}
87
88	return nil
89}