package music

import (
	"fmt"
	"path/filepath"
	"strings"
)

type DatasetConfig struct {
	Source      string `json:"source" yaml:"source"`
	Prefix      string `json:"prefix,omitempty" yaml:"prefix,omitempty"`
	StageRoot   string `json:"stage_root,omitempty" yaml:"stage_root,omitempty"`
	CreditsRoot string `json:"credits_root,omitempty" yaml:"credits_root,omitempty"`
}

type DefaultsConfig struct {
	StageRoot      string `json:"stage_root,omitempty" yaml:"stage_root,omitempty"`
	CreditsRoot    string `json:"credits_root,omitempty" yaml:"credits_root,omitempty"`
	CreditsOverlay string `json:"credits_overlay,omitempty" yaml:"credits_overlay,omitempty"`
	MaxStemLength  *int   `json:"max_stem_length,omitempty" yaml:"max_stem_length,omitempty"`
}

func ValidateDatasetSource(field, source string) error {
	trimmed := strings.TrimSpace(source)
	if trimmed == "" {
		return fmt.Errorf("%s.source is required", field)
	}
	if strings.Contains(trimmed, "\x00") {
		return fmt.Errorf("%s.source must not contain NUL bytes", field)
	}
	clean := filepath.Clean(filepath.FromSlash(trimmed))
	if clean == ".." || strings.HasPrefix(clean, ".."+string(filepath.Separator)) {
		return fmt.Errorf("%s.source must not escape project root", field)
	}
	return nil
}

func ValidateDatasetID(id string) error {
	trimmed := strings.TrimSpace(id)
	if trimmed == "" {
		return fmt.Errorf("dataset id must not be empty")
	}
	if strings.Contains(trimmed, "/") || strings.Contains(trimmed, "\\") {
		return fmt.Errorf("dataset id %q must not contain path separators", trimmed)
	}
	return nil
}
