.PHONY: help darwin-build darwin-switch nixos-build nixos-switch home-build home-switch update clean fmt check

# Default target
help:
	@echo "Available targets:"
	@echo "  make darwin-build      - Build darwin configuration (macOS)"
	@echo "  make darwin-switch     - Build and activate darwin configuration"
	@echo "  make nixos-build       - Build NixOS configuration (Linux)"
	@echo "  make nixos-switch      - Build and activate NixOS configuration"
	@echo "  make home-build        - Build home-manager configuration (standalone)"
	@echo "  make home-switch       - Build and activate home-manager configuration"
	@echo "  make update            - Update flake inputs"
	@echo "  make fmt               - Format nix files"
	@echo "  make check             - Check flake"
	@echo "  make clean             - Clean build artifacts and old generations"
	@echo ""
	@echo "Note: Set HOSTNAME variable to use a specific host, e.g.:"
	@echo "  make darwin-switch HOSTNAME=macbook"

# Auto-detect hostname
HOSTNAME ?= $(shell hostname | cut -d. -f1)
USERNAME ?= $(shell whoami)

# macOS targets
darwin-build:
	nix build .#darwinConfigurations.$(HOSTNAME).system

darwin-switch:
	darwin-rebuild switch --flake .#$(HOSTNAME)

# NixOS targets
nixos-build:
	nixos-rebuild build --flake .#$(HOSTNAME)

nixos-switch:
	sudo nixos-rebuild switch --flake .#$(HOSTNAME)

# Home Manager targets (standalone)
home-build:
	home-manager build --flake .#$(USERNAME)@$(HOSTNAME)

home-switch:
	home-manager switch --flake .#$(USERNAME)@$(HOSTNAME)

# Maintenance
update:
	nix flake update

fmt:
	nix fmt

check:
	nix flake check

clean:
	@echo "Cleaning old generations..."
	@if [ "$$(uname)" = "Darwin" ]; then \
		nix-collect-garbage -d; \
		darwin-rebuild --list-generations | tail -5; \
	else \
		sudo nix-collect-garbage -d; \
		sudo nix-env --list-generations --profile /nix/var/nix/profiles/system | tail -5; \
	fi

# Git helpers (optional)
.PHONY: commit-update
commit-update:
	git add flake.lock
	git commit -m "chore: update flake inputs"

