.PHONY: help release release-zip clean

# Default target
help:
	@echo "DahasaCakes Web Release Tools"
	@echo ""
	@echo "Available targets:"
	@echo "  make release VERSION=1.0.0      - Build and package as .zip"
	@echo "  make release-zip VERSION=1.0.0  - Same as 'release'"
	@echo "  make clean                      - Clean build artifacts"
	@echo ""

# Release packaging (creates .zip file for better Windows compatibility)
release: release-zip

release-zip: clean
	@if [ -z "$(VERSION)" ]; then \
		echo "Error: VERSION is required. Usage: make release VERSION=1.0.0"; \
		exit 1; \
	fi
	@echo "Building DahasaCakes Web v$(VERSION)..."
	@echo ""

	# Check if zip is installed
	@command -v zip >/dev/null || { echo "Error: 'zip' command not found. Install with: apt-get install zip"; exit 1; }

	# Build the application
	npm run build
	@echo "✓ Application built"

	# Create release directory
	@mkdir -p releases
	@mkdir -p dahasacakes-web-$(VERSION)

	# Copy files to release directory
	@echo "✓ Preparing release files..."
	@cp -r src prisma public dahasacakes-web-$(VERSION)/
	@cp -r docs dahasacakes-web-$(VERSION)/
	@cp package.json package-lock.json tsconfig.json next.config.ts postcss.config.mjs dahasacakes-web-$(VERSION)/
	@cp .env.example README.md Makefile RELEASE.md dahasacakes-web-$(VERSION)/
	@cp install.sh uninstall.sh dahasacakes-web-$(VERSION)/
	@chmod +x dahasacakes-web-$(VERSION)/install.sh
	@chmod +x dahasacakes-web-$(VERSION)/uninstall.sh

	# Create .zip file (better for Windows users)
	@echo "✓ Creating ZIP package..."
	@cd dahasacakes-web-$(VERSION) && zip -r -q ../releases/dahasacakes-web-$(VERSION).zip . \
		--exclude "*.git*" \
		--exclude "node_modules/*" \
		--exclude ".next/*" \
		--exclude ".env.local" \
		--exclude "dist/*" \
		--exclude ".vercel/*" \
		--exclude ".DS_Store"

	@cd releases && zip -q dahasacakes-web-$(VERSION).zip -d "dahasacakes-web-$(VERSION)/.git*" 2>/dev/null || true

	# Generate checksum for integrity verification
	@echo "✓ Generating SHA256 checksum..."
	@cd releases && shasum -a 256 dahasacakes-web-$(VERSION).zip > dahasacakes-web-$(VERSION).zip.sha256

	# Cleanup temp directory
	@rm -rf dahasacakes-web-$(VERSION)

	# Display completion info
	@echo ""
	@echo "╔════════════════════════════════════════════════════════════╗"
	@echo "║            Release Package Ready!                          ║"
	@echo "╚════════════════════════════════════════════════════════════╝"
	@echo ""
	@echo "📦 Release Files:"
	@ls -lh releases/dahasacakes-web-$(VERSION).zip*
	@echo ""
	@echo "📋 SHA256 Checksum:"
	@cat releases/dahasacakes-web-$(VERSION).zip.sha256
	@echo ""
	@echo "📝 Next Steps:"
	@echo "   1. Upload .zip file to releases server"
	@echo "   2. Upload .sha256 checksum for verification"
	@echo "   3. Update website download page with v$(VERSION)"
	@echo "   4. Tag release: git tag v$(VERSION) && git push origin v$(VERSION)"
	@echo ""
	@echo "💾 Users can now:"
	@echo "   1. Download: dahasacakes-web-$(VERSION).zip"
	@echo "   2. Extract: unzip dahasacakes-web-$(VERSION).zip"
	@echo "   3. Install: cd dahasacakes-web && chmod +x install.sh && ./install.sh"
	@echo ""

# Clean build artifacts
clean:
	@echo "Cleaning build artifacts..."
	@rm -rf .next
	@rm -rf dist
	@rm -rf dahasacakes-web-*
	@echo "✓ Clean complete"
