BINARY_NAME=skyview
BUILD_DIR=build
VERSION=$(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")

.PHONY: build clean run dev test lint deb deb-clean install-deps

build:
	@echo "Building $(BINARY_NAME)..."
	@mkdir -p $(BUILD_DIR)
	go build -ldflags="-w -s -X main.version=$(VERSION)" -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/skyview

clean:
	@echo "Cleaning..."
	@rm -rf $(BUILD_DIR)
	go clean

run: build
	@echo "Running $(BINARY_NAME)..."
	@./$(BUILD_DIR)/$(BINARY_NAME)

dev:
	@echo "Running in development mode..."
	go run ./cmd/skyview

test:
	@echo "Running tests..."
	go test ./...

lint:
	@echo "Running linter..."
	@if command -v golangci-lint > /dev/null 2>&1; then \
		golangci-lint run; \
	else \
		echo "golangci-lint not installed, skipping lint"; \
	fi

# Debian package targets
deb:
	@echo "Building Debian package..."
	@./scripts/build-deb.sh

deb-clean:
	@echo "Cleaning Debian package artifacts..."
	@rm -f debian/usr/bin/skyview
	@rm -rf build/*.deb

deb-install: deb
	@echo "Installing Debian package..."
	@if [ "$$EUID" -ne 0 ]; then \
		echo "Please run as root: sudo make deb-install"; \
		exit 1; \
	fi
	@dpkg -i build/skyview_*.deb || (apt-get update && apt-get -f install -y)

deb-remove:
	@echo "Removing Debian package..."
	@if [ "$$EUID" -ne 0 ]; then \
		echo "Please run as root: sudo make deb-remove"; \
		exit 1; \
	fi
	@dpkg -r skyview || true

# Docker/Podman targets
docker-build:
	@echo "Building Docker image..."
	docker build -t skyview .

podman-build:
	@echo "Building Podman image..."
	podman build -t skyview .

# Development targets
install-deps:
	@echo "Installing Go dependencies..."
	go mod tidy

format:
	@echo "Formatting code..."
	go fmt ./...

vet:
	@echo "Running go vet..."
	go vet ./...

# Combined quality check
check: format vet lint test
	@echo "All checks passed!"

.DEFAULT_GOAL := build