package services

import (
	"encoding/json"
	"os"
)

// ReadModelsConfig reads models.json as a raw string.
func ReadModelsConfig(path string) (string, error) {
	data, err := os.ReadFile(path)
	if os.IsNotExist(err) {
		return "{}", nil
	}
	return string(data), err
}

// WriteModelsConfig validates JSON and writes models.json.
func WriteModelsConfig(path, content string) error {
	var v any
	if err := json.Unmarshal([]byte(content), &v); err != nil {
		return err
	}
	pretty, err := json.MarshalIndent(v, "", "  ")
	if err != nil {
		return err
	}
	return os.WriteFile(path, append(pretty, '\n'), 0o644)
}
