# felhom-controller Makefile
# Build targets for amd64 (N100 mini PCs) and arm64 (Raspberry Pi)

VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
BUILD_TIME ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ)

BINARY = felhom-controller
REGISTRY ?= gitea.dooplex.hu/admin
IMAGE = $(REGISTRY)/felhom-controller

LDFLAGS = -ldflags="-s -w \
	-X main.Version=$(VERSION) \
	-X main.BuildTime=$(BUILD_TIME) \
	-X main.GitCommit=$(GIT_COMMIT)"

.PHONY: all build build-amd64 build-arm64 build-all test lint clean \
        docker-build docker-push docker-buildx run sync-assets

# Default: build for current platform
all: build

build:
	go build $(LDFLAGS) -o bin/$(BINARY) ./cmd/controller/

build-amd64:
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o bin/$(BINARY)-linux-amd64 ./cmd/controller/

build-arm64:
	CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build $(LDFLAGS) -o bin/$(BINARY)-linux-arm64 ./cmd/controller/

build-all: build-amd64 build-arm64

# Run locally (for development)
run:
	go run ./cmd/controller/ --config configs/controller.yaml.example

# Tests
test:
	go test -v ./...

test-cover:
	go test -coverprofile=coverage.out ./...
	go tool cover -html=coverage.out -o coverage.html

# Lint (requires golangci-lint)
lint:
	golangci-lint run ./...

# Docker image (current platform)
docker-build:
	docker build \
		--build-arg VERSION=$(VERSION) \
		--build-arg GIT_COMMIT=$(GIT_COMMIT) \
		-t $(IMAGE):$(VERSION) \
		-t $(IMAGE):latest \
		.

# Docker multi-arch build (requires docker buildx)
docker-buildx:
	docker buildx build \
		--platform linux/amd64,linux/arm64 \
		--build-arg VERSION=$(VERSION) \
		--build-arg GIT_COMMIT=$(GIT_COMMIT) \
		-t $(IMAGE):$(VERSION) \
		-t $(IMAGE):latest \
		--push \
		.

# Push docker image
docker-push:
	docker push $(IMAGE):$(VERSION)
	docker push $(IMAGE):latest

# Sync app assets from felhom.eu website repo
# Override with: make sync-assets WEBSITE_ASSETS_DIR=/path/to/website/assets
WEBSITE_ASSETS_DIR ?= ../felhom.eu/website/assets

sync-assets:
	@echo "Syncing assets from $(WEBSITE_ASSETS_DIR)..."
	@if [ ! -d "$(WEBSITE_ASSETS_DIR)" ]; then \
		echo "ERROR: $(WEBSITE_ASSETS_DIR) not found."; \
		echo "Set WEBSITE_ASSETS_DIR to the path containing app logos and screenshots."; \
		exit 1; \
	fi
	@cp -v $(WEBSITE_ASSETS_DIR)/*-logo.svg assets/ 2>/dev/null || echo "No SVG logo files found"
	@cp -v $(WEBSITE_ASSETS_DIR)/*-logo.png assets/ 2>/dev/null || echo "No PNG logo files found"
	@cp -v $(WEBSITE_ASSETS_DIR)/*-screenshot-*.webp assets/ 2>/dev/null || echo "No screenshot files found"
	@echo "Assets synced: $$(ls assets/*.svg assets/*.png assets/*.webp 2>/dev/null | wc -l) files"

# Generate bcrypt password hash (usage: make password PASS=mypassword)
password:
	@go run -mod=mod golang.org/x/crypto/bcrypt 2>/dev/null || \
	echo '$$2a$$10$$...' && echo "Install htpasswd or use: go run scripts/hashpass.go $(PASS)"

# Clean build artifacts
clean:
	rm -rf bin/ coverage.out coverage.html
