.PHONY: generate frontend backend backend-linux backend-windows backend-darwin \
	build build-linux build-windows build-darwin clean clean-all dist run

.NOTPARALLEL:

GO ?= go
NPM ?= npm
GOPROXY ?= https://goproxy.cn,direct
DIST_DIR := dist
BINARY_NAME := home-vue-go
WINDOWS_TEMP := $(BINARY_NAME)-windows-amd64.exe
LINUX_TEMP := $(BINARY_NAME)-linux-amd64
DARWIN_TEMP := $(BINARY_NAME)-darwin-amd64
ALL_WINDOWS_TEMP := $(BINARY_NAME)-all-windows-amd64.exe
ALL_LINUX_TEMP := $(BINARY_NAME)-all-linux-amd64
ALL_DARWIN_TEMP := $(BINARY_NAME)-all-darwin-amd64

ifeq ($(OS),Windows_NT)
NPM_CMD := npm.cmd
else
NPM_CMD := $(NPM)
endif

# Generated Ent sources are committed. Run this target only after schema changes.
generate:
	@echo [generate] Updating Ent sources...
ifeq ($(OS),Windows_NT)
	@cd internal\ent && set "GOPROXY=$(GOPROXY)" && set "CGO_ENABLED=0" && set "GOARCH=amd64" && $(GO) generate ./...
else
	@cd internal/ent && GOPROXY="$(GOPROXY)" $(GO) generate ./...
endif
	@echo [generate] Done.

# Install missing packages without deleting a usable local dependency tree.
frontend:
	@echo [frontend] Installing dependencies...
	@$(NPM_CMD) install --prefer-offline --no-audit --no-fund
	@echo [frontend] Building production assets...
	@$(NPM_CMD) run build

# Backend-only targets keep frontend files beside the executable for debugging.
backend-windows: frontend
	@echo [backend] Building Windows amd64 executable...
ifeq ($(OS),Windows_NT)
	@set "GOPROXY=$(GOPROXY)" && set "CGO_ENABLED=0" && set "GOOS=windows" && set "GOARCH=amd64" && $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(DIST_DIR)\$(BINARY_NAME).exe .
else
	@GOPROXY="$(GOPROXY)" CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(DIST_DIR)/$(BINARY_NAME).exe .
endif

backend-linux: frontend
	@echo [backend] Building Linux amd64 executable...
ifeq ($(OS),Windows_NT)
	@set "GOPROXY=$(GOPROXY)" && set "CGO_ENABLED=0" && set "GOOS=linux" && set "GOARCH=amd64" && $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(DIST_DIR)\$(BINARY_NAME) .
else
	@GOPROXY="$(GOPROXY)" CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(DIST_DIR)/$(BINARY_NAME) .
	@chmod +x $(DIST_DIR)/$(BINARY_NAME)
endif

backend-darwin: frontend
	@echo [backend] Building macOS amd64 executable...
ifeq ($(OS),Windows_NT)
	@set "GOPROXY=$(GOPROXY)" && set "CGO_ENABLED=0" && set "GOOS=darwin" && set "GOARCH=amd64" && $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(DIST_DIR)\$(BINARY_NAME) .
else
	@GOPROXY="$(GOPROXY)" CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(DIST_DIR)/$(BINARY_NAME) .
	@chmod +x $(DIST_DIR)/$(BINARY_NAME)
endif

backend: frontend
	@echo [backend] Building for the current platform...
ifeq ($(OS),Windows_NT)
	@set "GOPROXY=$(GOPROXY)" && set "CGO_ENABLED=0" && $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(DIST_DIR)\$(BINARY_NAME).exe .
else
	@GOPROXY="$(GOPROXY)" CGO_ENABLED=0 $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(DIST_DIR)/$(BINARY_NAME) .
	@chmod +x $(DIST_DIR)/$(BINARY_NAME)
endif

# Full builds embed frontend assets, then remove the duplicate loose files.
build-windows: clean frontend
	@echo [build] Building Windows amd64 package...
ifeq ($(OS),Windows_NT)
	@set "GOPROXY=$(GOPROXY)" && set "CGO_ENABLED=0" && set "GOOS=windows" && set "GOARCH=amd64" && $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(WINDOWS_TEMP) .
	@if exist $(DIST_DIR) rmdir /s /q $(DIST_DIR)
	@mkdir $(DIST_DIR)
	@move /y $(WINDOWS_TEMP) $(DIST_DIR)\$(BINARY_NAME).exe >nul
else
	@GOPROXY="$(GOPROXY)" CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(WINDOWS_TEMP) .
	@rm -rf $(DIST_DIR) && mkdir -p $(DIST_DIR)
	@mv $(WINDOWS_TEMP) $(DIST_DIR)/$(BINARY_NAME).exe
endif
	@echo [build] Output: $(DIST_DIR)/$(BINARY_NAME).exe

build-linux: clean frontend
	@echo [build] Building Linux amd64 package...
ifeq ($(OS),Windows_NT)
	@set "GOPROXY=$(GOPROXY)" && set "CGO_ENABLED=0" && set "GOOS=linux" && set "GOARCH=amd64" && $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(LINUX_TEMP) .
	@if exist $(DIST_DIR) rmdir /s /q $(DIST_DIR)
	@mkdir $(DIST_DIR)
	@move /y $(LINUX_TEMP) $(DIST_DIR)\$(BINARY_NAME) >nul
else
	@GOPROXY="$(GOPROXY)" CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(LINUX_TEMP) .
	@rm -rf $(DIST_DIR) && mkdir -p $(DIST_DIR)
	@mv $(LINUX_TEMP) $(DIST_DIR)/$(BINARY_NAME)
	@chmod +x $(DIST_DIR)/$(BINARY_NAME)
endif
	@echo [build] Output: $(DIST_DIR)/$(BINARY_NAME)

build-darwin: clean frontend
	@echo [build] Building macOS amd64 package...
ifeq ($(OS),Windows_NT)
	@set "GOPROXY=$(GOPROXY)" && set "CGO_ENABLED=0" && set "GOOS=darwin" && set "GOARCH=amd64" && $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(DARWIN_TEMP) .
	@if exist $(DIST_DIR) rmdir /s /q $(DIST_DIR)
	@mkdir $(DIST_DIR)
	@move /y $(DARWIN_TEMP) $(DIST_DIR)\$(BINARY_NAME) >nul
else
	@GOPROXY="$(GOPROXY)" CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(DARWIN_TEMP) .
	@rm -rf $(DIST_DIR) && mkdir -p $(DIST_DIR)
	@mv $(DARWIN_TEMP) $(DIST_DIR)/$(BINARY_NAME)
	@chmod +x $(DIST_DIR)/$(BINARY_NAME)
endif
	@echo [build] Output: $(DIST_DIR)/$(BINARY_NAME)

build: clean frontend
	@echo [build] Building Windows amd64 package...
ifeq ($(OS),Windows_NT)
	@set "GOPROXY=$(GOPROXY)" && set "CGO_ENABLED=0" && set "GOOS=windows" && set "GOARCH=amd64" && $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(ALL_WINDOWS_TEMP) .
	@echo [build] Building Linux amd64 package...
	@set "GOPROXY=$(GOPROXY)" && set "CGO_ENABLED=0" && set "GOOS=linux" && set "GOARCH=amd64" && $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(ALL_LINUX_TEMP) .
	@echo [build] Building macOS amd64 package...
	@set "GOPROXY=$(GOPROXY)" && set "CGO_ENABLED=0" && set "GOOS=darwin" && set "GOARCH=amd64" && $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(ALL_DARWIN_TEMP) .
	@if exist $(DIST_DIR) rmdir /s /q $(DIST_DIR)
	@mkdir $(DIST_DIR)
	@move /y $(ALL_WINDOWS_TEMP) $(DIST_DIR)\$(BINARY_NAME)-windows-amd64.exe >nul
	@move /y $(ALL_LINUX_TEMP) $(DIST_DIR)\$(BINARY_NAME)-linux-amd64 >nul
	@move /y $(ALL_DARWIN_TEMP) $(DIST_DIR)\$(BINARY_NAME)-darwin-amd64 >nul
else
	@GOPROXY="$(GOPROXY)" CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(ALL_WINDOWS_TEMP) .
	@echo [build] Building Linux amd64 package...
	@GOPROXY="$(GOPROXY)" CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(ALL_LINUX_TEMP) .
	@echo [build] Building macOS amd64 package...
	@GOPROXY="$(GOPROXY)" CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GO) build -trimpath -buildvcs=false -ldflags="-s -w" -o $(ALL_DARWIN_TEMP) .
	@rm -rf $(DIST_DIR) && mkdir -p $(DIST_DIR)
	@mv $(ALL_WINDOWS_TEMP) $(DIST_DIR)/$(BINARY_NAME)-windows-amd64.exe
	@mv $(ALL_LINUX_TEMP) $(DIST_DIR)/$(BINARY_NAME)-linux-amd64
	@mv $(ALL_DARWIN_TEMP) $(DIST_DIR)/$(BINARY_NAME)-darwin-amd64
	@chmod +x $(DIST_DIR)/$(BINARY_NAME)-linux-amd64 $(DIST_DIR)/$(BINARY_NAME)-darwin-amd64
endif
	@echo [build] Output: $(DIST_DIR)/$(BINARY_NAME)-windows-amd64.exe
	@echo [build] Output: $(DIST_DIR)/$(BINARY_NAME)-linux-amd64
	@echo [build] Output: $(DIST_DIR)/$(BINARY_NAME)-darwin-amd64

dist: build

run: frontend
ifeq ($(OS),Windows_NT)
	@set "GOPROXY=$(GOPROXY)" && set "CGO_ENABLED=0" && $(GO) run .
else
	@GOPROXY="$(GOPROXY)" CGO_ENABLED=0 $(GO) run .
endif

clean:
	@echo [clean] Removing build artifacts...
ifeq ($(OS),Windows_NT)
	@if exist $(DIST_DIR) rmdir /s /q $(DIST_DIR)
	@if exist $(BINARY_NAME).exe del /f /q $(BINARY_NAME).exe
	@if exist $(WINDOWS_TEMP) del /f /q $(WINDOWS_TEMP)
	@if exist $(LINUX_TEMP) del /f /q $(LINUX_TEMP)
	@if exist $(DARWIN_TEMP) del /f /q $(DARWIN_TEMP)
	@if exist $(ALL_WINDOWS_TEMP) del /f /q $(ALL_WINDOWS_TEMP)
	@if exist $(ALL_LINUX_TEMP) del /f /q $(ALL_LINUX_TEMP)
	@if exist $(ALL_DARWIN_TEMP) del /f /q $(ALL_DARWIN_TEMP)
else
	@rm -rf $(DIST_DIR)
	@rm -f $(BINARY_NAME) $(BINARY_NAME).exe $(WINDOWS_TEMP) $(LINUX_TEMP) $(DARWIN_TEMP) $(ALL_WINDOWS_TEMP) $(ALL_LINUX_TEMP) $(ALL_DARWIN_TEMP)
endif
	@echo [clean] Done.

clean-all: clean
	@echo [clean] Removing frontend dependencies...
ifeq ($(OS),Windows_NT)
	@if exist node_modules rmdir /s /q node_modules
else
	@rm -rf node_modules
endif
	@echo [clean] Dependency cleanup complete.
