// Path: Code/Tracker/config/config.go

package config

import (
	"os"

	"gopkg.in/yaml.v3"
)

type Config struct {
	Watch    WatchConfig    `yaml:"watch"`
	Services ServicesConfig `yaml:"services"`
	Ollama   OllamaConfig   `yaml:"ollama"`
	Tracker  TrackerConfig  `yaml:"tracker"`
	Database DatabaseConfig `yaml:"database"`
	Logging  LoggingConfig  `yaml:"logging"`
}

type WatchConfig struct {
	Root             string   `yaml:"root"`
	Extensions       []string `yaml:"extensions"`
	Exclude          []string `yaml:"exclude"`
	StabilitySeconds int      `yaml:"stability_seconds"`
}

type ServicesConfig struct {
	Docling string `yaml:"docling"`
	Ollama  string `yaml:"ollama"`
	Tools   string `yaml:"tools"`
}

type OllamaConfig struct {
	Model       string  `yaml:"model"`
	Temperature float64 `yaml:"temperature"`
}

type TrackerConfig struct {
	Path      string `yaml:"path"`
	Sheet     string `yaml:"sheet"`
	HeaderRow int    `yaml:"header_row"`
}

type DatabaseConfig struct {
	Path string `yaml:"path"`
}

type LoggingConfig struct {
	Level     string `yaml:"level"`
	ErrorFile string `yaml:"error_file"`
}

func Load(path string) (*Config, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		return nil, err
	}

	var cfg Config
	if err := yaml.Unmarshal(data, &cfg); err != nil {
		return nil, err
	}

	// Set defaults
	if cfg.Watch.StabilitySeconds == 0 {
		cfg.Watch.StabilitySeconds = 120
	}
	if cfg.Tracker.HeaderRow == 0 {
		cfg.Tracker.HeaderRow = 1
	}

	return &cfg, nil
}
