TOML_FILE := Cargo.toml

# Extract version from TOML
VERSION := $(shell sed -n 's/^version *= *"\(.*\)"/\1/p' $(TOML_FILE))

# Default: bump patch
PATCH_VERSION := $(shell \
	echo $(VERSION) | awk -F. '{printf "%d.%d.%d", $$1, $$2, $$3+1}' \
)

# Allow override
NEW_VERSION ?= $(PATCH_VERSION)

.PHONY: version tag release


version:
	@echo "Current version: $(VERSION)"
	# Update version in Cargo.toml
	@sed -i.bak 's/^version *= *".*"/version = "$(NEW_VERSION)"/' $(TOML_FILE)
	@rm -f $(TOML_FILE).bak
	@echo "Release version: $(NEW_VERSION)"
	cargo check
tag:
	@git tag -a v$(NEW_VERSION) -m "Release v$(NEW_VERSION)"
	@git push origin v$(NEW_VERSION)

package:
	@echo packaging crate
	git add $(TOML_FILE) Cargo.lock
	@git commit -m "Bump version to v$(NEW_VERSION)"
	@git push
	echo added git
	@cargo package

release: version package tag
	@echo "Creating GitHub release v$(NEW_VERSION)"
	@gh release create v$(NEW_VERSION) \
		--title "v$(NEW_VERSION)" \
		--notes "Release v$(NEW_VERSION)"
	@echo "Creating crate release v$(NEW_VERSION)"
	@cargo publish

lint: 
	cargo clippy --all-features --all-targets --examples --tests -- -D warnings 
fmt:
	cargo fmt --all
	cargo clippy --all-features --all-targets --examples --tests --fix --allow-dirty -- -D warnings
build:
	cargo build --all-features
test:
	cargo test --all-features
run:
	cargo run --all-features

codegen:
	bash build_scripts/pre_processing.sh
	python build_scripts/patch_spec.py

	redocly bundle ws_messages.json -o ws_spec_updated.json
	openapi-generator-cli generate \
	  -i ws_spec_updated.json \
	  -g rust \
	  -o ./generated \
	  --type-mappings decimal=rust_decimal::Decimal \
	--additional-properties=supportAsync=true,useSingleRequestParameter=true,avoidBoxedModels=true \
	--skip-validate-spec
	cp ./generated/src/models/* ./src/models/
	rm -rf ./generated ws_spec_updated.json

	openapi-generator-cli generate \
	  -i openapi.json \
	  -g rust \
	  -o ./generated \
	  --type-mappings decimal=rust_decimal::Decimal \
	--additional-properties=supportAsync=true,useSingleRequestParameter=true,avoidBoxedModels=true

	cp ./generated/src/models/* ./src/models/
	cp -r ./generated/src/apis ./src/

	# rebuild mod.rs
	@echo "#![allow(clippy::all)]" > ./src/models/mod.rs
	@echo "#![allow(unused_imports)]" >> ./src/models/mod.rs
	@echo "#![allow(dead_code)]" >> ./src/models/mod.rs
	@echo "#![allow(non_camel_case_types)]" >> ./src/models/mod.rs
	@echo "#![allow(clippy::upper_case_acronyms)]" >> ./src/models/mod.rs
	# rebuild api mod.rs

	@echo "#![allow(clippy::all)]" > ./src/apis/mod.rs
	cat ./generated/src/apis/mod.rs >> ./src/apis/mod.rs

	# cleanup
	rm -rf ./generated

	@for f in ./src/models/*.rs; do \
		base=$$(basename $$f); \
		if [ "$$base" = "mod.rs" ]; then continue; fi; \
		name=$${base%.rs}; \
		camel=$$(echo $$name | sed -E 's/(^|_)([a-z])/\U\2/g'); \
		echo "pub mod $$name;" >> ./src/models/mod.rs; \
		echo "pub use $$name::$$camel;" >> ./src/models/mod.rs; \
	done
	python build_scripts/post_processing.py

docs:
	python build_scripts/readme.py

all: codegen fmt lint build test docs
	@echo "All tasks completed successfully."
