package config

import (
	"os"
	"time"

	"gopkg.in/yaml.v2"
)

type Config struct {
	Containers []ContainerConfig `yaml:"containers"`
	Global     GlobalConfig      `yaml:"global"`
}

type GlobalConfig struct {
	ReconcileInterval   time.Duration `yaml:"reconcile_interval"`
	HealthCheckInterval time.Duration `yaml:"health_check_interval"`
	CleanupInterval     time.Duration `yaml:"cleanup_interval"`
}

type ContainerConfig struct {
	Name          string            `yaml:"name"`
	Image         string            `yaml:"image"`
	Command       []string          `yaml:"command,omitempty"`
	Env           map[string]string `yaml:"env,omitempty"`
	RestartPolicy string            `yaml:"restart_policy,omitempty"`
	HealthCheck   *HealthCheck      `yaml:"health_check,omitempty"`
	Volumes       []string          `yaml:"volumes,omitempty"`
	Ports         []string          `yaml:"ports,omitempty"`
	NetworkMode   string            `yaml:"network_mode,omitempty"`
}

type HealthCheck struct {
	Test        []string      `yaml:"test"`
	Interval    time.Duration `yaml:"interval"`
	Timeout     time.Duration `yaml:"timeout"`
	Retries     int           `yaml:"retries"`
	StartPeriod time.Duration `yaml:"start_period"`
}

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

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

	return &config, nil
}
