package kb

import (
	"fmt"
	"strings"
)

const (
	SourceMirrorUpstreamTypeGenericGit = "generic_git"
	SourceMirrorUpstreamTypeGitHub     = "github"
	SourceMirrorUpstreamTypeGitea      = "gitea"
	SourceMirrorUpstreamTypeGitLab     = "gitlab"
	SourceMirrorUpstreamTypeGogs       = "gogs"
	SourceMirrorUpstreamTypeOneDev     = "onedev"
	SourceMirrorUpstreamTypeGitBucket  = "gitbucket"
	SourceMirrorUpstreamTypeCodebase   = "codebase"
	SourceMirrorUpstreamTypeCodeCommit = "codecommit"
)

const (
	SourceMirrorSyncStatusIdle     = "idle"
	SourceMirrorSyncStatusQueued   = "queued"
	SourceMirrorSyncStatusRunning  = "running"
	SourceMirrorSyncStatusSuccess  = "success"
	SourceMirrorSyncStatusFailed   = "failed"
	SourceMirrorSyncStatusDisabled = "disabled"
)

// SourceMirrorUpstreamPreset describes a supported external Git upstream type.
// The worker remains a unified Git clone/fetch pipeline; presets are for stable
// validation and future provider-specific UX/import metadata.
type SourceMirrorUpstreamPreset struct {
	Type        string
	Label       string
	URLExample  string
	AuthModes   []string
	Description string
}

var sourceMirrorUpstreamPresets = []SourceMirrorUpstreamPreset{
	{Type: SourceMirrorUpstreamTypeGenericGit, Label: "Generic Git", URLExample: "https://git.example.com/org/repo.git", AuthModes: []string{"none", "token", "ssh_key"}, Description: "Plain Git remote"},
	{Type: SourceMirrorUpstreamTypeGitHub, Label: "GitHub", URLExample: "https://github.com/org/repo.git", AuthModes: []string{"none", "token"}, Description: "GitHub.com repository"},
	{Type: SourceMirrorUpstreamTypeGitea, Label: "Gitea", URLExample: "https://gitea.example.com/org/repo.git", AuthModes: []string{"none", "token", "ssh_key"}, Description: "Gitea or self-hosted Gitea"},
	{Type: SourceMirrorUpstreamTypeGitLab, Label: "GitLab", URLExample: "https://gitlab.com/org/repo.git", AuthModes: []string{"none", "token", "ssh_key"}, Description: "GitLab or self-hosted GitLab"},
}

// SourceMirrorUpstreamPresets returns the supported upstream presets in stable UI order.
func SourceMirrorUpstreamPresets() []SourceMirrorUpstreamPreset {
	presets := make([]SourceMirrorUpstreamPreset, len(sourceMirrorUpstreamPresets))
	copy(presets, sourceMirrorUpstreamPresets)
	return presets
}

// NormalizeSourceMirrorUpstreamType trims and defaults a mirror upstream type.
func NormalizeSourceMirrorUpstreamType(upstreamType string) string {
	trimmed := strings.TrimSpace(upstreamType)
	if trimmed == "" {
		return SourceMirrorUpstreamTypeGenericGit
	}
	return trimmed
}

// IsSupportedSourceMirrorUpstreamType reports whether upstreamType is a supported v1 mirror upstream.
func IsSupportedSourceMirrorUpstreamType(upstreamType string) bool {
	candidate := NormalizeSourceMirrorUpstreamType(upstreamType)
	for _, preset := range sourceMirrorUpstreamPresets {
		if preset.Type == candidate {
			return true
		}
	}
	return false
}

// ValidateSourceMirrorUpstreamType returns the normalized upstream type or an error.
func ValidateSourceMirrorUpstreamType(upstreamType string) (string, error) {
	normalized := NormalizeSourceMirrorUpstreamType(upstreamType)
	if IsSupportedSourceMirrorUpstreamType(normalized) {
		return normalized, nil
	}
	return "", fmt.Errorf("unsupported source mirror upstream type %q", strings.TrimSpace(upstreamType))
}
