.PHONY: build test lint clean run fmt vet build-restic fetch-restic

# Binary name
BINARY_NAME=privatebackup-backend

# Build flags
BUILD_FLAGS=-ldflags="-s -w"

# Restic source directory and version
RESTIC_DIR=external/restic
RESTIC_REPO=https://github.com/restic/restic.git
RESTIC_VERSION=v0.17.3

# Embedded binaries directory
EMBED_DIR=internal/restic/embedded

# Default target
all: test build

# Fetch restic source if submodule is empty (supports both git clone and source archives)
fetch-restic:
	@if [ -z "$$(ls -A $(RESTIC_DIR) 2>/dev/null)" ]; then \
		echo "Restic source not found, fetching $(RESTIC_VERSION)..."; \
		rm -rf $(RESTIC_DIR); \
		git clone --depth 1 --branch $(RESTIC_VERSION) $(RESTIC_REPO) $(RESTIC_DIR); \
	else \
		echo "Restic source already present"; \
	fi

# Build restic binary from submodule for current platform
build-restic: fetch-restic
	@echo "Building restic from submodule..."
	@mkdir -p $(EMBED_DIR)
	cd $(RESTIC_DIR) && go build -ldflags="-s -w" -o ../../$(EMBED_DIR)/restic ./cmd/restic
	@echo "Restic built successfully"

# Build restic for a specific platform (usage: make build-restic-linux-amd64)
build-restic-linux-amd64: fetch-restic
	@echo "Building restic for linux/amd64..."
	@mkdir -p $(EMBED_DIR)
	cd $(RESTIC_DIR) && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ../../$(EMBED_DIR)/restic-linux-amd64 ./cmd/restic

build-restic-linux-arm64: fetch-restic
	@echo "Building restic for linux/arm64..."
	@mkdir -p $(EMBED_DIR)
	cd $(RESTIC_DIR) && CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o ../../$(EMBED_DIR)/restic-linux-arm64 ./cmd/restic

build-restic-darwin-amd64: fetch-restic
	@echo "Building restic for darwin/amd64..."
	@mkdir -p $(EMBED_DIR)
	cd $(RESTIC_DIR) && CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o ../../$(EMBED_DIR)/restic-darwin-amd64 ./cmd/restic

build-restic-darwin-arm64: fetch-restic
	@echo "Building restic for darwin/arm64..."
	@mkdir -p $(EMBED_DIR)
	cd $(RESTIC_DIR) && CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o ../../$(EMBED_DIR)/restic-darwin-arm64 ./cmd/restic

build-restic-windows-amd64: fetch-restic
	@echo "Building restic for windows/amd64..."
	@mkdir -p $(EMBED_DIR)
	cd $(RESTIC_DIR) && CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o ../../$(EMBED_DIR)/restic-windows-amd64.exe ./cmd/restic

# Build restic for all platforms
build-restic-all: build-restic-linux-amd64 build-restic-linux-arm64 build-restic-darwin-amd64 build-restic-darwin-arm64 build-restic-windows-amd64

# Build the binary (requires restic to be built first for target platform)
build: build-restic
	go build $(BUILD_FLAGS) -o bin/$(BINARY_NAME) ./cmd/client

# Build for all platforms (builds restic for each platform first)
build-all: build-restic-all
	GOOS=linux GOARCH=amd64 go build $(BUILD_FLAGS) -o bin/$(BINARY_NAME)-linux-amd64 ./cmd/client
	GOOS=linux GOARCH=arm64 go build $(BUILD_FLAGS) -o bin/$(BINARY_NAME)-linux-arm64 ./cmd/client
	GOOS=darwin GOARCH=amd64 go build $(BUILD_FLAGS) -o bin/$(BINARY_NAME)-darwin-amd64 ./cmd/client
	GOOS=darwin GOARCH=arm64 go build $(BUILD_FLAGS) -o bin/$(BINARY_NAME)-darwin-arm64 ./cmd/client
	GOOS=windows GOARCH=amd64 go build $(BUILD_FLAGS) -o bin/$(BINARY_NAME)-windows-amd64.exe ./cmd/client

# Run tests
test:
	go test -v -race -coverprofile=coverage.out ./...

# Run tests with coverage report
test-coverage: test
	go tool cover -html=coverage.out -o coverage.html

# Run linting
lint:
	golangci-lint run ./...

# Format code
fmt:
	go fmt ./...

# Vet code
vet:
	go vet ./...

# Clean build artifacts
clean:
	rm -rf bin/
	rm -rf $(EMBED_DIR)/
	rm -f coverage.out coverage.html

# Run the binary
run: build
	./bin/$(BINARY_NAME)

# Install dependencies
deps:
	go mod download
	go mod tidy

# Initialize/update submodules
submodules:
	git submodule update --init --recursive

# Update restic submodule to latest release
update-restic:
	cd $(RESTIC_DIR) && git fetch --tags && git checkout $$(git describe --tags $$(git rev-list --tags --max-count=1))
	@echo "Updated restic to: $$(cd $(RESTIC_DIR) && git describe --tags)"

# Generate mocks (if needed)
generate:
	go generate ./...
