# QA STLC Agents — Makefile
#
# Supports ADO, Jira, and Both pipelines.
#
# INTEGRATION variable controls which agents/skills/config are active.
# Override on the command line:
#   make install INTEGRATION=jira
#   make mcp-config INTEGRATION=both
#   make verify INTEGRATION=jira
#
# Convenience aliases (no INTEGRATION override needed):
#   make install-ado      make install-jira      make install-both
#   make mcp-config-ado   make mcp-config-jira   make mcp-config-both
#   make skills-ado       make skills-jira        make skills-both
#   make verify-ado       make verify-jira        make verify-both

PYTHON      := python3
VENV        := .venv
PIP         := $(VENV)/bin/pip
PYTEST      := $(VENV)/bin/pytest
INTEGRATION ?= ado

# ── Agent binary paths ────────────────────────────────────────────────────────
MCP_TC   := $(VENV)/bin/qa-test-case-manager
MCP_GH   := $(VENV)/bin/qa-gherkin-generator
MCP_PW   := $(VENV)/bin/qa-playwright-generator
MCP_HX   := $(VENV)/bin/qa-helix-writer
MCP_JR   := $(VENV)/bin/qa-jira-manager

.PHONY: install install-ado install-jira install-both install-dev \
        skills skills-ado skills-jira skills-both skills-vscode \
        mcp-config mcp-config-ado mcp-config-jira mcp-config-both \
        verify verify-ado verify-jira verify-both \
        test clean

# ─── Install ─────────────────────────────────────────────────────────────────
# All five agents ship from the same pip package — one install always covers all.
install:
	$(PYTHON) -m venv $(VENV)
	$(PIP) install --upgrade pip
	$(PIP) install -e .
	@echo ""
	@echo "✓ All five MCP agents installed:"
	@echo "  ADO  entry point : $(MCP_TC)"
	@echo "  Jira entry point : $(MCP_JR)"
	@echo "  Shared (both)    : $(MCP_GH)"
	@echo "                     $(MCP_PW)"
	@echo "                     $(MCP_HX)"
	@echo ""
	@echo "Active integration : $(INTEGRATION)"
	@$(MAKE) --no-print-directory _print_next_steps_$(INTEGRATION)

install-ado:  ; @$(MAKE) install INTEGRATION=ado
install-jira: ; @$(MAKE) install INTEGRATION=jira
install-both: ; @$(MAKE) install INTEGRATION=both

install-dev: install
	$(PIP) install pytest pytest-asyncio

# ─── Internal next-step hints ─────────────────────────────────────────────────
_print_next_steps_ado:
	@echo "Next: make mcp-config-ado && make skills-ado"
	@echo "      npx @playwright/mcp@latest --port 8931"

_print_next_steps_jira:
	@echo "Next: make mcp-config-jira && make skills-jira"
	@echo "      npx @playwright/mcp@latest --port 8931"
	@echo "      Add JIRA_CLIENT_ID / JIRA_CLIENT_SECRET / JIRA_CLOUD_ID to .env"

_print_next_steps_both:
	@echo "Next: make mcp-config-both && make skills-both"
	@echo "      npx @playwright/mcp@latest --port 8931"
	@echo "      Add JIRA_CLIENT_ID / JIRA_CLIENT_SECRET / JIRA_CLOUD_ID to .env"

# ─── Verify ───────────────────────────────────────────────────────────────────
verify:
	@echo "=== Verifying integration: $(INTEGRATION) ==="
ifeq ($(INTEGRATION),ado)
	@$(MAKE) --no-print-directory _verify_shared
	@$(MAKE) --no-print-directory _verify_ado_auth
else ifeq ($(INTEGRATION),jira)
	@$(MAKE) --no-print-directory _verify_shared
	@$(MAKE) --no-print-directory _verify_jira_auth
else ifeq ($(INTEGRATION),both)
	@$(MAKE) --no-print-directory _verify_shared
	@$(MAKE) --no-print-directory _verify_ado_auth
	@$(MAKE) --no-print-directory _verify_jira_auth
else
	@echo "Unknown INTEGRATION=$(INTEGRATION). Use ado, jira, or both."
	@exit 1
endif

_verify_shared:
	@echo "--- Shared agents ---"
	@test -f $(MCP_GH) && echo "✓ $(MCP_GH)" || echo "✗ $(MCP_GH) not found — run: make install"
	@test -f $(MCP_PW) && echo "✓ $(MCP_PW)" || echo "✗ $(MCP_PW) not found — run: make install"
	@test -f $(MCP_HX) && echo "✓ $(MCP_HX)" || echo "✗ $(MCP_HX) not found — run: make install"

_verify_ado_auth:
	@echo "--- ADO agent + MSAL auth ---"
	@test -f $(MCP_TC) && echo "✓ $(MCP_TC)" || echo "✗ $(MCP_TC) not found — run: make install"
	@$(PYTHON) -c "from stlc_agents.shared.auth import get_signed_in_user; u=get_signed_in_user(); print('✓ ADO MSAL: signed in as ' + u if u else '⚠ ADO MSAL: no cached account — browser opens on first tool call')" 2>/dev/null || echo "⚠ ADO MSAL: auth module not found — run: make install"

_verify_jira_auth:
	@echo "--- Jira agent + OAuth cache ---"
	@test -f $(MCP_JR) && echo "✓ $(MCP_JR)" || echo "✗ $(MCP_JR) not found — run: make install"
	@test -f ~/.jira-cache/jira-token.json && echo "✓ Jira token cache: ~/.jira-cache/jira-token.json" || echo "⚠ Jira token cache not found — browser opens on first qa-jira-manager call"
	@test -n "$$JIRA_CLIENT_ID" && echo "✓ JIRA_CLIENT_ID set" || echo "⚠ JIRA_CLIENT_ID not set in env / .env"

verify-ado:  ; @$(MAKE) verify INTEGRATION=ado
verify-jira: ; @$(MAKE) verify INTEGRATION=jira
verify-both: ; @$(MAKE) verify INTEGRATION=both

# ─── Skills ──────────────────────────────────────────────────────────────────
# Shared ADO skill dirs
ADO_SKILL_DIRS = generate-test-cases generate-gherkin generate-playwright-code \
                 write-helix-files deduplication-protocol
# Shared Jira skill dirs (no generate-test-cases; qa-jira-manager replaces it)
JIRA_SKILL_DIRS = qa-jira-manager generate-gherkin generate-playwright-code \
                  write-helix-files deduplication-protocol

# Claude Code — copies entire skill/<name>/ → .claude/skills/<name>/ (preserves references/)
skills:
	@echo "Installing skills (integration=$(INTEGRATION)) → .claude/skills/ ..."
	@mkdir -p .claude/skills
	@cp skills/AGENT-BEHAVIOR.md .claude/
ifeq ($(INTEGRATION),ado)
	@for d in $(ADO_SKILL_DIRS); do cp -r skills/$$d .claude/skills/; done
else ifeq ($(INTEGRATION),jira)
	@for d in $(JIRA_SKILL_DIRS); do cp -r skills/$$d .claude/skills/; done
else ifeq ($(INTEGRATION),both)
	@for d in $(ADO_SKILL_DIRS) qa-jira-manager; do cp -r skills/$$d .claude/skills/; done
else
	@echo "Unknown INTEGRATION=$(INTEGRATION). Use ado, jira, or both."
	@exit 1
endif
	@echo "✓ Skills installed to .claude/skills/ (integration=$(INTEGRATION))"

# VS Code / GitHub Copilot — flat copy: SKILL.md → .github/copilot-instructions/<name>.md
skills-vscode:
	@echo "Installing skills (integration=$(INTEGRATION)) → .github/copilot-instructions/ ..."
	@mkdir -p .github/copilot-instructions
	@cp skills/AGENT-BEHAVIOR.md .github/copilot-instructions/
ifeq ($(INTEGRATION),ado)
	@for d in $(ADO_SKILL_DIRS); do cp skills/$$d/SKILL.md .github/copilot-instructions/$$d.md; done
else ifeq ($(INTEGRATION),jira)
	@for d in $(JIRA_SKILL_DIRS); do cp skills/$$d/SKILL.md .github/copilot-instructions/$$d.md; done
else ifeq ($(INTEGRATION),both)
	@for d in $(ADO_SKILL_DIRS) qa-jira-manager; do cp skills/$$d/SKILL.md .github/copilot-instructions/$$d.md; done
else
	@echo "Unknown INTEGRATION=$(INTEGRATION). Use ado, jira, or both."
	@exit 1
endif
	@echo "✓ Skills installed to .github/copilot-instructions/ (integration=$(INTEGRATION))"

skills-ado:   ; @$(MAKE) skills skills-vscode INTEGRATION=ado
skills-jira:  ; @$(MAKE) skills skills-vscode INTEGRATION=jira
skills-both:  ; @$(MAKE) skills skills-vscode INTEGRATION=both

# ─── MCP Config ──────────────────────────────────────────────────────────────
mcp-config:
	@echo "Writing .mcp.json (integration=$(INTEGRATION)) ..."
	@ABS=$$(pwd); \
	case "$(INTEGRATION)" in \
	  ado) \
	    printf '{\n  "mcpServers": {\n    "qa-test-case-manager":    { "command": "%s" },\n    "qa-gherkin-generator":    { "command": "%s" },\n    "qa-playwright-generator": { "command": "%s" },\n    "qa-helix-writer":         { "command": "%s" },\n    "playwright":              { "type": "url", "url": "ws://localhost:8931" }\n  }\n}\n' \
	      "$$ABS/$(MCP_TC)" "$$ABS/$(MCP_GH)" "$$ABS/$(MCP_PW)" "$$ABS/$(MCP_HX)" > .mcp.json ;; \
	  jira) \
	    printf '{\n  "mcpServers": {\n    "qa-jira-manager":          { "command": "%s", "env": { "JIRA_CLIENT_ID": "${JIRA_CLIENT_ID}", "JIRA_CLIENT_SECRET": "${JIRA_CLIENT_SECRET}", "JIRA_CLOUD_ID": "${JIRA_CLOUD_ID}" } },\n    "qa-gherkin-generator":    { "command": "%s" },\n    "qa-playwright-generator": { "command": "%s" },\n    "qa-helix-writer":         { "command": "%s" },\n    "playwright":              { "type": "url", "url": "ws://localhost:8931" }\n  }\n}\n' \
	      "$$ABS/$(MCP_JR)" "$$ABS/$(MCP_GH)" "$$ABS/$(MCP_PW)" "$$ABS/$(MCP_HX)" > .mcp.json ;; \
	  both) \
	    printf '{\n  "mcpServers": {\n    "qa-test-case-manager":    { "command": "%s" },\n    "qa-jira-manager":          { "command": "%s", "env": { "JIRA_CLIENT_ID": "${JIRA_CLIENT_ID}", "JIRA_CLIENT_SECRET": "${JIRA_CLIENT_SECRET}", "JIRA_CLOUD_ID": "${JIRA_CLOUD_ID}" } },\n    "qa-gherkin-generator":    { "command": "%s" },\n    "qa-playwright-generator": { "command": "%s" },\n    "qa-helix-writer":         { "command": "%s" },\n    "playwright":              { "type": "url", "url": "ws://localhost:8931" }\n  }\n}\n' \
	      "$$ABS/$(MCP_TC)" "$$ABS/$(MCP_JR)" "$$ABS/$(MCP_GH)" "$$ABS/$(MCP_PW)" "$$ABS/$(MCP_HX)" > .mcp.json ;; \
	  *) echo "Unknown INTEGRATION=$(INTEGRATION). Use ado, jira, or both." && exit 1 ;; \
	esac
	@echo "✓ .mcp.json written (integration=$(INTEGRATION))"

mcp-config-ado:  ; @$(MAKE) mcp-config INTEGRATION=ado
mcp-config-jira: ; @$(MAKE) mcp-config INTEGRATION=jira
mcp-config-both: ; @$(MAKE) mcp-config INTEGRATION=both

# ─── Test ─────────────────────────────────────────────────────────────────────
test:
	$(PYTEST) tests/ -v

# ─── Clean ────────────────────────────────────────────────────────────────────
clean:
	rm -rf $(VENV) __pycache__ dist *.egg-info
	find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
	find . -type f -name "*.pyc" -delete
	@echo "✓ Cleaned"
