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

export PYTHONPATH := scripts:$(PYTHONPATH)
VENV := .venv
PYTHON := $(VENV)/bin/python3
CREDENTIALS := .secret/fuj-management-bot-credentials.json

GO_SRC := go
GO_BIN := bin/fuj

$(PYTHON): .venv/.last_sync

.venv/.last_sync: pyproject.toml
	uv sync
	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 Python dashboard (alias for web-py, until M8)"
	@echo "  make web-py      - Start Python dashboard on :5001"
	@echo "  make web-go      - Build and start Go dashboard on :8080"
	@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 tests"
	@echo "  make go-lint     - Run golangci-lint on Go code"
	@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 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

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

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

web: web-py

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

web-debug: $(PYTHON)
	FLASK_DEBUG=1 $(PYTHON) 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-run: go-build
	./$(GO_BIN) $(ARGS)

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

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

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 build/Dockerfile .

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

sync: $(PYTHON)
	$(PYTHON) scripts/sync_fio_to_sheets.py --credentials .secret/fuj-management-bot-credentials.json

sync-2025: $(PYTHON)
	$(PYTHON) scripts/sync_fio_to_sheets.py --credentials .secret/fuj-management-bot-credentials.json --from 2025-10-01 --to 2025-12-31 --sort-by-date

sync-2026: $(PYTHON)
	$(PYTHON) scripts/sync_fio_to_sheets.py --credentials .secret/fuj-management-bot-credentials.json --from 2026-01-01 --to 2026-12-31 --sort-by-date

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

reconcile:  ## Match payments against attendance
	export PYTHONPATH=$(PYTHONPATH):$(CURDIR)/scripts && $(PYTHON) scripts/match_payments.py --credentials $(CREDENTIALS)

test: $(PYTHON) ## Run web application tests
	export PYTHONPATH=$(PYTHONPATH):$(CURDIR)/scripts:$(CURDIR) && $(PYTHON) -m unittest discover tests

test-v: $(PYTHON) ## Run tests with verbose output
	export PYTHONPATH=$(PYTHONPATH):$(CURDIR)/scripts:$(CURDIR) && $(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
