package config

import (
	"fmt"
	"os"

	"gopkg.in/yaml.v3"
)

// Config is the site-level configuration loaded from config.yaml.
type Config struct {
	Title       string `yaml:"title"`
	Hostname    string `yaml:"hostname"`
	BaseURL     string `yaml:"baseURL"`
	Theme       string `yaml:"theme"`
	Description string `yaml:"description"`
	Author      string `yaml:"author"`
	Paginate    int    `yaml:"paginate"`
}

func Load(path string) (Config, error) {
	var cfg Config
	b, err := os.ReadFile(path)
	if err != nil {
		return cfg, fmt.Errorf("read config %s: %w", path, err)
	}
	if err := yaml.Unmarshal(b, &cfg); err != nil {
		return cfg, fmt.Errorf("parse config %s: %w", path, err)
	}
	if cfg.Title == "" {
		return cfg, fmt.Errorf("config: title is required")
	}
	if cfg.Theme == "" {
		cfg.Theme = "default"
	}
	if cfg.Hostname == "" {
		cfg.Hostname = cfg.Title
	}
	return cfg, nil
}
