# Local parity with CI checks
# Usage examples:
#   make ci            # run full pipeline (format check, analyze, test + coverage gate)
#   make format        # apply formatting in-place
#   make format-check  # fail if formatting needed
#   make analyze       # static analysis
#   make test          # tests with coverage output
#   make coverage-check              # gate coverage (locked to 100%)

SHELL := /bin/bash
MIN_COV := 100
.ONESHELL:

.PHONY: ci bootstrap format format-check analyze test coverage coverage-check publish-dry-run clean

ci:
	@set +e
	echo "== format-check"
	dart format --output=none --set-exit-if-changed .
	fmt=$$?
	echo "== analyze"
	flutter analyze
	ana=$$?
	echo "== test"
	flutter test --coverage
	tst=$$?
	echo "== coverage-check"
	$(MAKE) coverage-check
	cov=$$?
	echo "== publish-dry-run"
	flutter pub publish --dry-run
	pdry=$$?
	echo ""
	echo "Summary:"
	printf "%-18s %s\n" "Step" "Status"
	printf "%-18s %s\n" "format-check" $$([ $$fmt -eq 0 ] && echo PASS || echo FAIL)
	printf "%-18s %s\n" "analyze" $$([ $$ana -eq 0 ] && echo PASS || echo FAIL)
	printf "%-18s %s\n" "test" $$([ $$tst -eq 0 ] && echo PASS || echo FAIL)
	printf "%-18s %s\n" "coverage-check" $$([ $$cov -eq 0 ] && echo PASS || echo FAIL)
	printf "%-18s %s\n" "publish-dry-run" $$([ $$pdry -eq 0 ] && echo PASS || echo FAIL)
	if [ $$fmt -ne 0 ] || [ $$ana -ne 0 ] || [ $$tst -ne 0 ] || [ $$cov -ne 0 ] || [ $$pdry -ne 0 ]; then exit 1; fi

bootstrap:
	flutter pub get

format:
	dart format .

format-check:
	dart format --output=none --set-exit-if-changed . || { \
		echo "Formatting required. Run 'dart format .' locally and commit the changes." ; \
		exit 1 ; \
	}

analyze:
	flutter analyze

test:
	flutter test --coverage

coverage-check:
	@awk -F':' ' \
	  /^LF:/{lf+=$$2} \
	  /^LH:/{lh+=$$2} \
	  END { \
	    if (lf == 0) { \
	      print "No coverage data found."; \
	      exit 1; \
	    } \
	    cov = (lh/lf)*100; \
	    printf("Total Coverage: %.2f%%\n", cov); \
	    if (cov < $(MIN_COV)) { \
	      printf("Code coverage is less than %d%%!\n", $(MIN_COV)); \
	      exit 1; \
	    } else { \
	      printf(">= %d%% code coverage achieved!\n", $(MIN_COV)); \
	    } \
	  }' coverage/lcov.info

# Load environments from .env file if it exists
ifneq (,$(wildcard ./.env))
    include .env
    export
endif

publish-dry-run:
	flutter pub publish --dry-run

publish:
	@VERSION=$$(grep '^version: ' pubspec.yaml | awk '{print $$2}' | tr -d '\r') ; \
	if [ -z "$$VERSION" ]; then \
		echo "Error: Could not extract version from pubspec.yaml."; \
		exit 1; \
	fi; \
	echo "== Found version $$VERSION in pubspec.yaml" ; \
	echo "== Creating git tag..." ; \
	git tag -a v$$VERSION -m "Release v$$VERSION" || echo "Tag v$$VERSION already exists" ; \
	echo "== Publishing to pub.dev..." ; \
	if [ -n "$$PUB_TOKEN" ]; then \
		dart pub token add https://pub.dev --env-var PUB_TOKEN; \
	fi ; \
	flutter pub publish --force ; \
	echo "== Pushing tags..." ; \
	git push origin main --tags

clean:
	rm -rf coverage .dart_tool
