// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package actions

// CompatibilityStatus represents the support status of an action
type CompatibilityStatus string

const (
	CompatibilityFull    CompatibilityStatus = "full"
	CompatibilityPartial CompatibilityStatus = "partial"
	CompatibilityNone    CompatibilityStatus = "none"
)

// ActionCompatibility represents compatibility information for a GitHub action
type ActionCompatibility struct {
	// Action name (e.g., "actions/checkout")
	Name string
	// Supported versions
	Versions []string
	// Compatibility status
	Status CompatibilityStatus
	// Notes about compatibility
	Notes string
	// Suggested alternative if not fully compatible
	Alternative string
}

// BuiltinCompatibility contains known action compatibility information for Gitea Actions
// This is used by the capability discovery API to help AI tools write correct workflows
var BuiltinCompatibility = map[string]*ActionCompatibility{
	"actions/checkout": {
		Name:     "actions/checkout",
		Versions: []string{"v2", "v3", "v4"},
		Status:   CompatibilityFull,
		Notes:    "Fully compatible with Gitea Actions",
	},
	"actions/setup-node": {
		Name:     "actions/setup-node",
		Versions: []string{"v2", "v3", "v4"},
		Status:   CompatibilityFull,
		Notes:    "Fully compatible with Gitea Actions",
	},
	"actions/setup-go": {
		Name:     "actions/setup-go",
		Versions: []string{"v3", "v4", "v5"},
		Status:   CompatibilityFull,
		Notes:    "Fully compatible with Gitea Actions",
	},
	"actions/setup-python": {
		Name:     "actions/setup-python",
		Versions: []string{"v4", "v5"},
		Status:   CompatibilityFull,
		Notes:    "Fully compatible with Gitea Actions",
	},
	"actions/setup-java": {
		Name:     "actions/setup-java",
		Versions: []string{"v3", "v4"},
		Status:   CompatibilityFull,
		Notes:    "Fully compatible with Gitea Actions",
	},
	"actions/setup-dotnet": {
		Name:     "actions/setup-dotnet",
		Versions: []string{"v3", "v4"},
		Status:   CompatibilityFull,
		Notes:    "Fully compatible with Gitea Actions",
	},
	"actions/upload-artifact": {
		Name:        "actions/upload-artifact",
		Versions:    []string{"v2", "v3"},
		Status:      CompatibilityPartial,
		Notes:       "v4 not supported on GHES-compatible runners. Use v3 or Gitea API for artifact upload.",
		Alternative: "actions/upload-artifact@v3 or direct Gitea API upload",
	},
	"actions/download-artifact": {
		Name:        "actions/download-artifact",
		Versions:    []string{"v2", "v3"},
		Status:      CompatibilityPartial,
		Notes:       "v4 not supported on GHES-compatible runners. Use v3.",
		Alternative: "actions/download-artifact@v3",
	},
	"actions/cache": {
		Name:     "actions/cache",
		Versions: []string{"v3", "v4"},
		Status:   CompatibilityFull,
		Notes:    "Fully compatible with Gitea Actions",
	},
	"actions/github-script": {
		Name:     "actions/github-script",
		Versions: []string{"v6", "v7"},
		Status:   CompatibilityPartial,
		Notes:    "GitHub API calls may not work. Use for basic scripting only.",
	},
}

// UnsupportedFeatures lists features that are not supported in Gitea Actions
var UnsupportedFeatures = []string{
	"GitHub-hosted runners",
	"Environments with protection rules",
	"OIDC token authentication",
	"Required workflows",
	"Deployment branches",
	"Reusable workflows from other repositories (limited)",
	"actions/upload-artifact@v4",
	"actions/download-artifact@v4",
}

// IncompatibleActions maps action@version to error messages
var IncompatibleActions = map[string]string{
	"actions/upload-artifact@v4":   "v4 not supported on Gitea/GHES-compatible runners. Use actions/upload-artifact@v3 or direct Gitea API upload.",
	"actions/download-artifact@v4": "v4 not supported on Gitea/GHES-compatible runners. Use actions/download-artifact@v3.",
}

// GetCompatibility returns compatibility information for an action
func GetCompatibility(actionName string) *ActionCompatibility {
	return BuiltinCompatibility[actionName]
}

// GetIncompatibilityMessage returns an error message if the action@version is incompatible
func GetIncompatibilityMessage(actionWithVersion string) string {
	return IncompatibleActions[actionWithVersion]
}

// IsActionCompatible checks if an action@version is compatible with Gitea Actions
func IsActionCompatible(actionWithVersion string) bool {
	_, incompatible := IncompatibleActions[actionWithVersion]
	return !incompatible
}

// GetSuggestedAlternative returns a suggested alternative for an incompatible action
func GetSuggestedAlternative(actionWithVersion string) string {
	alternatives := map[string]string{
		"actions/upload-artifact@v4":   "uses: actions/upload-artifact@v3",
		"actions/download-artifact@v4": "uses: actions/download-artifact@v3",
	}
	return alternatives[actionWithVersion]
}
