# Release Management Makefile Integration
# Include this file in your main Makefile to add release management capabilities
#
# Usage: include capabilities/release-management/release.mk

# Release Management Variables
RELEASE_MANAGEMENT_PATH := capabilities/release-management
RELEASE_CLI := release

# Check if release management capability is available
RELEASE_AVAILABLE := $(shell command -v $(RELEASE_CLI) 2> /dev/null)

# Release Status and Information
.PHONY: release-status
release-status: ## Show current release status and version information
ifndef RELEASE_AVAILABLE
	@echo "❌ Release management capability not installed"
	@echo "   Install with: pip install -e $(RELEASE_MANAGEMENT_PATH)/"
	@exit 1
endif
	$(RELEASE_CLI) status

.PHONY: release-validate
release-validate: ## Validate repository state for release readiness
ifndef RELEASE_AVAILABLE
	@echo "❌ Release management capability not installed"
	@exit 1
endif
	$(RELEASE_CLI) validate

.PHONY: release-registry-info
release-registry-info: ## Show package registry information and status
ifndef RELEASE_AVAILABLE
	@echo "❌ Release management capability not installed"
	@exit 1
endif
	$(RELEASE_CLI) registry-info

# Git Tag Management
.PHONY: release-tag
release-tag: ## Create git tag for version (requires VERSION=x.y.z)
ifndef VERSION
	@echo "❌ VERSION is required. Usage: make release-tag VERSION=1.0.0"
	@exit 1
endif
ifndef RELEASE_AVAILABLE
	@echo "❌ Release management capability not installed"
	@exit 1
endif
	$(RELEASE_CLI) tag --version $(VERSION)

# Package Building
.PHONY: release-build
release-build: ## Build release packages using setuptools-scm
ifndef RELEASE_AVAILABLE
	@echo "❌ Release management capability not installed"
	@exit 1
endif
	$(RELEASE_CLI) build

.PHONY: release-clean
release-clean: ## Clean build artifacts and temporary files
ifndef RELEASE_AVAILABLE
	@echo "❌ Release management capability not installed"
	@exit 1
endif
	$(RELEASE_CLI) clean

# Publishing Workflows
.PHONY: release-publish
release-publish: ## Complete release workflow: tag + build (requires VERSION=x.y.z)
ifndef VERSION
	@echo "❌ VERSION is required. Usage: make release-publish VERSION=1.0.0"
	@exit 1
endif
ifndef RELEASE_AVAILABLE
	@echo "❌ Release management capability not installed"
	@exit 1
endif
	$(RELEASE_CLI) publish --version $(VERSION)

.PHONY: release-publish-gitea
release-publish-gitea: ## Complete release workflow + Gitea upload (requires VERSION=x.y.z)
ifndef VERSION
	@echo "❌ VERSION is required. Usage: make release-publish-gitea VERSION=1.0.0"
	@exit 1
endif
ifndef RELEASE_AVAILABLE
	@echo "❌ Release management capability not installed"
	@exit 1
endif
	$(RELEASE_CLI) publish --version $(VERSION) --registry gitea

.PHONY: release-publish-pypi
release-publish-pypi: ## Complete release workflow + PyPI upload (requires VERSION=x.y.z)
ifndef VERSION
	@echo "❌ VERSION is required. Usage: make release-publish-pypi VERSION=1.0.0"
	@exit 1
endif
ifndef RELEASE_AVAILABLE
	@echo "❌ Release management capability not installed"
	@exit 1
endif
	$(RELEASE_CLI) publish --version $(VERSION) --registry pypi

# Upload Existing Packages
.PHONY: release-upload-gitea
release-upload-gitea: ## Upload existing packages to Gitea registry
ifndef RELEASE_AVAILABLE
	@echo "❌ Release management capability not installed"
	@exit 1
endif
	$(RELEASE_CLI) upload --registry gitea

.PHONY: release-upload-pypi
release-upload-pypi: ## Upload existing packages to PyPI
ifndef RELEASE_AVAILABLE
	@echo "❌ Release management capability not installed"
	@exit 1
endif
	$(RELEASE_CLI) upload --registry pypi

.PHONY: release-upload-testpypi
release-upload-testpypi: ## Upload existing packages to Test PyPI
ifndef RELEASE_AVAILABLE
	@echo "❌ Release management capability not installed"
	@exit 1
endif
	$(RELEASE_CLI) upload --registry testpypi

# Dry Run Options
.PHONY: release-publish-dry-run
release-publish-dry-run: ## Dry run of complete release workflow (requires VERSION=x.y.z)
ifndef VERSION
	@echo "❌ VERSION is required. Usage: make release-publish-dry-run VERSION=1.0.0"
	@exit 1
endif
ifndef RELEASE_AVAILABLE
	@echo "❌ Release management capability not installed"
	@exit 1
endif
	$(RELEASE_CLI) publish --version $(VERSION) --dry-run

.PHONY: release-upload-dry-run
release-upload-dry-run: ## Dry run of package upload to default registry
ifndef RELEASE_AVAILABLE
	@echo "❌ Release management capability not installed"
	@exit 1
endif
	$(RELEASE_CLI) upload --dry-run

# Development and Setup
.PHONY: release-management-install
release-management-install: ## Install release management capability
	pip install -e $(RELEASE_MANAGEMENT_PATH)/

.PHONY: release-management-install-dev
release-management-install-dev: ## Install release management capability with dev dependencies
	pip install -e "$(RELEASE_MANAGEMENT_PATH)/[dev]"

.PHONY: release-management-test
release-management-test: ## Run release management capability tests
	cd $(RELEASE_MANAGEMENT_PATH) && pytest tests/

.PHONY: release-management-help
release-management-help: ## Show release management CLI help
ifndef RELEASE_AVAILABLE
	@echo "❌ Release management capability not installed"
	@echo "   Install with: make release-management-install"
	@exit 1
endif
	$(RELEASE_CLI) --help

# Help target integration
.PHONY: help-release
help-release: ## Show release management specific help
	@echo ""
	@echo "📦 Release Management:"
	@echo "  release-status               Show current release status and version information"
	@echo "  release-validate             Validate repository state for release readiness"
	@echo "  release-registry-info        Show package registry information and status"
	@echo ""
	@echo "🏷️  Git Tag Management:"
	@echo "  release-tag VERSION=x.y.z    Create git tag for version"
	@echo ""
	@echo "🔨 Package Building:"
	@echo "  release-build                Build release packages using setuptools-scm"
	@echo "  release-clean                Clean build artifacts and temporary files"
	@echo ""
	@echo "🚀 Publishing Workflows:"
	@echo "  release-publish VERSION=x.y.z         Complete release workflow (tag + build)"
	@echo "  release-publish-gitea VERSION=x.y.z   Complete release + Gitea upload"
	@echo "  release-publish-pypi VERSION=x.y.z    Complete release + PyPI upload"
	@echo ""
	@echo "📤 Upload Existing Packages:"
	@echo "  release-upload-gitea         Upload existing packages to Gitea registry"
	@echo "  release-upload-pypi          Upload existing packages to PyPI"
	@echo "  release-upload-testpypi      Upload existing packages to Test PyPI"
	@echo ""
	@echo "🧪 Dry Run Options:"
	@echo "  release-publish-dry-run VERSION=x.y.z  Dry run of release workflow"
	@echo "  release-upload-dry-run                  Dry run of package upload"
	@echo ""
	@echo "⚙️  Development and Setup:"
	@echo "  release-management-install          Install release management capability"
	@echo "  release-management-install-dev      Install with development dependencies"
	@echo "  release-management-test             Run capability tests"
	@echo "  release-management-help             Show CLI help"
	@echo ""

# Default registry shortcuts (can be overridden)
RELEASE_DEFAULT_REGISTRY ?= gitea

.PHONY: release-upload
release-upload: release-upload-$(RELEASE_DEFAULT_REGISTRY) ## Upload packages to default registry ($(RELEASE_DEFAULT_REGISTRY))

# Integration with main project targets
# These can be overridden in main Makefile if different behavior is needed

.PHONY: package
package: release-build ## Build packages (alias for release-build)

.PHONY: publish
publish: ## Publish release to default registry (requires VERSION=x.y.z)
ifndef VERSION
	@echo "❌ VERSION is required. Usage: make publish VERSION=1.0.0"
	@exit 1
endif
	@make release-publish-$(RELEASE_DEFAULT_REGISTRY) VERSION=$(VERSION)

# Legacy compatibility targets
.PHONY: release-status-legacy
release-status-legacy: release-status ## Legacy alias for release-status

.PHONY: package-upload
package-upload: release-upload ## Legacy alias for release-upload