.PHONY: help fees match web web-py web-debug web-go go-build go-test go-test-all go-parity go-run go-sync-debug go-lint capture-fixtures parity image run sync sync-2026 sync-year test test-v docs

# Legacy Python implementation lives in legacy/ (Go in go/ is primary).
LEGACY := legacy
export PYTHONPATH := $(CURDIR)/$(LEGACY)/scripts:$(CURDIR)/$(LEGACY):$(PYTHONPATH)
VENV := $(LEGACY)/.venv
PYTHON := $(CURDIR)/$(VENV)/bin/python3
CREDENTIALS := $(CURDIR)/.secret/fuj-management-bot-credentials.json
# Both implementations read CREDENTIALS_PATH; the Python default would resolve under legacy/.
export CREDENTIALS_PATH ?= $(CREDENTIALS)

GO_SRC := go
GO_BIN := bin/fuj

$(PYTHON): $(VENV)/.last_sync

$(VENV)/.last_sync: $(LEGACY)/pyproject.toml
	uv sync --project $(LEGACY)
	touch $(VENV)/.last_sync

help:
	@echo "Available targets:"
	@echo "  make fees        - Calculate monthly fees from the attendance sheet"
	@echo "  make match       - Match Fio bank payments against expected attendance fees"
	@echo "  make web         - Start legacy Python dashboard (alias for web-py)"
	@echo "  make web-py      - Start legacy Python dashboard on :5001"
	@echo "  make web-go      - Build and start Go dashboard on :8080 (primary)"
	@echo "  make web-debug   - Start Python dashboard in debug mode"
	@echo "  make go-build    - Build Go binary to bin/fuj"
	@echo "  make go-test     - Run Go unit tests"
	@echo "  make go-parity   - Run Go parity tests (requires -tags=parity fixture corpus)"
	@echo "  make go-test-all - Run both unit and parity tests"
	@echo "  make go-lint     - Run golangci-lint on Go code"
	@echo "  make go-sync-debug [DAYS=N] - Dry-run Go sync with Fio debug logs and txn table (default DAYS=30)"
	@echo "  make capture-fixtures - Regenerate parity fixture corpus from live Python"
	@echo "  make parity      - Diff /api/* between web-py (:5001) and web-go (:8080); both must be running"
	@echo "  make image       - Build Python OCI container image"
	@echo "  make run         - Run the built Python Docker image locally"
	@echo "  make sync        - Sync Fio transactions to Google Sheets"
	@echo "  make sync-2025   - Sync Fio transactions for Q4 2025 (Oct-Dec)"
	@echo "  make sync-2026   - Sync Fio transactions for the whole year of 2026"
	@echo "  make sync-year [YEAR=N] - Sync Fio transactions for YEAR plus Nov-Dec of the year before (default: current year)"
	@echo "  make sync-debug [DAYS=N] - Dry-run Python sync with Fio diagnostics and txn table (default DAYS=30)"
	@echo "  make infer       - Infer payment details (Person, Purpose, Amount) in the sheet"
	@echo "  make reconcile   - Show balance report using Google Sheets data"
	@echo "  make venv        - Sync virtual environment with pyproject.toml"
	@echo "  make test        - Run Python web application infrastructure tests"
	@echo "  make test-v      - Run Python tests with verbose output"
	@echo "  make docs        - Serve documentation in a browser"

venv:
	uv sync --project $(LEGACY)

fees: $(PYTHON)
	$(PYTHON) $(LEGACY)/scripts/calculate_fees.py

match: $(PYTHON)
	$(PYTHON) $(LEGACY)/scripts/match_payments.py

web: web-py

web-py: $(PYTHON)
	$(PYTHON) $(LEGACY)/app.py

web-debug: $(PYTHON)
	FLASK_DEBUG=1 $(PYTHON) $(LEGACY)/app.py

go-build:
	cd $(GO_SRC) && go build -trimpath \
	  -ldflags "-X main.version=$$(git describe --tags --always 2>/dev/null || echo dev) \
	            -X main.commit=$$(git rev-parse --short HEAD) \
	            -X main.buildDate=$$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
	  -o ../$(GO_BIN) ./cmd/fuj

go-test:
	cd $(GO_SRC) && go test -race ./...

go-parity:
	cd $(GO_SRC) && go test -tags=parity ./tests/parity/...

go-test-all: go-test go-parity

capture-fixtures: $(PYTHON)
	@echo "Capturing and scrubbing fixtures for all registered functions..."
	@for func in normalize parse_month_references calculate_fee calculate_junior_fee \
	              parse_czk_amount generate_sync_id build_name_variants match_members \
	              infer_transaction_details format_date reconcile; do \
	    dir="go/tests/fixtures/$$([[ $$func == reconcile ]] && echo reconcile || echo pure/$$func)"; \
	    mkdir -p "$$dir"; \
	    $(PYTHON) $(LEGACY)/scripts/capture_fixtures.py --func $$func --all \
	    | while IFS= read -r line; do \
	        case_id=$$(echo "$$line" | $(PYTHON) -c "import sys,json; print(json.load(sys.stdin)['case'])"); \
	        echo "$$line" | $(PYTHON) $(LEGACY)/scripts/scrub_fixtures.py > "$$dir/$${case_id}.json"; \
	    done; \
	    echo "  $$func done"; \
	done
	@echo "capture-fixtures complete."

go-run: go-build
	./$(GO_BIN) $(ARGS)

DAYS ?= 30
go-sync-debug: go-build
	LOG_LEVEL=DEBUG ./$(GO_BIN) sync -dry-run -print-fio-table -days $(DAYS)

go-lint:
	cd $(GO_SRC) && golangci-lint run ./...

web-go: go-build
	./$(GO_BIN) server

parity:
	cd $(GO_SRC) && go run ./cmd/parity $(ARGS)

image:
	docker build -t fuj-management:latest \
		--build-arg GIT_TAG=$$(git describe --tags --always 2>/dev/null || echo "untagged") \
		--build-arg GIT_COMMIT=$$(git rev-parse --short HEAD) \
		--build-arg BUILD_DATE=$$(date -u +%Y-%m-%dT%H:%M:%SZ) \
		-f $(LEGACY)/build/Dockerfile $(LEGACY)

run:
	docker run -it --rm -p 5001:5001 -v $(CURDIR)/.secret:/app/.secret:ro fuj-management:latest

sync: $(PYTHON)
	$(PYTHON) $(LEGACY)/scripts/sync_fio_to_sheets.py --credentials $(CREDENTIALS)

sync-2025: $(PYTHON)
	$(PYTHON) $(LEGACY)/scripts/sync_fio_to_sheets.py --credentials $(CREDENTIALS) --from 2025-10-01 --to 2025-12-31 --sort-by-date

sync-2026: $(PYTHON)
	$(PYTHON) $(LEGACY)/scripts/sync_fio_to_sheets.py --credentials $(CREDENTIALS) --from 2026-01-01 --to 2026-12-31 --sort-by-date

YEAR ?= $(shell date +%Y)
sync-year: $(PYTHON)
	$(PYTHON) $(LEGACY)/scripts/sync_fio_to_sheets.py --credentials $(CREDENTIALS) --from $(shell expr $(YEAR) - 1)-11-01 --to $(YEAR)-12-31 --sort-by-date

sync-debug: $(PYTHON)  ## Dry-run Python sync with Fio diagnostics and txn table (default DAYS=30)
	$(PYTHON) $(LEGACY)/scripts/sync_fio_to_sheets.py --credentials $(CREDENTIALS) --days $(DAYS) --dry-run --print-fio-table

infer: $(PYTHON)
	$(PYTHON) $(LEGACY)/scripts/infer_payments.py --credentials $(CREDENTIALS)

reconcile: $(PYTHON) ## Match payments against attendance
	$(PYTHON) $(LEGACY)/scripts/match_payments.py --credentials $(CREDENTIALS)

test: $(PYTHON) ## Run web application tests
	cd $(LEGACY) && $(PYTHON) -m unittest discover tests

test-v: $(PYTHON) ## Run tests with verbose output
	cd $(LEGACY) && $(PYTHON) -m unittest discover -v tests

docs: ## Serve documentation locally
	@echo "Starting documentation server at http://localhost:8000"
	@echo "Press Ctrl+C to stop."
	$(PYTHON) -m http.server 8000 --directory docs
