# Emoji
CHECK := ✅
BUILD := 🔨
CLEAN := 🧹
RUN := 🚀
CSS := 🎨
TEST := 🧪
DOCKER := 🐳
WARN := ⚠️
DOCS := 📚

# Variables
BINARY_NAME := atri.dad
DOCKER_IMAGE := atri-dot-dad
GO_FILES := $(wildcard *.go)
CSS_INPUT := lib/stylegen/base.css
CSS_OUTPUT := public/css/styles.css

# Docker detection
DOCKER_ENV := $(shell if [ -f /.dockerenv ]; then echo true; else echo false; fi)
ifeq ($(DOCKER_ENV),true)
    BASE_PATH := /app
else
    BASE_PATH := $(CURDIR)
endif

.PHONY: all build clean run dev stylegen docker-dev docker-build docker-run test help reset ensure-swag

help:
	@echo "Available commands:"
	@echo "make clean     - Remove build artifacts"
	@echo "make reset     - Clean and reset the project to initial state"
	@echo "make build     - Generate CSS, docs, and build Go binary"
	@echo "make run       - Build and run the binary"
	@echo "make dev       - Run in development mode"
	@echo "make docker-dev - Run development environment in Docker"
	@echo "make docker-run - Run production container"
	@echo "make test      - Run tests"
	@echo "make prod      - Full production build"

# Check if swag is installed
ensure-swag:
	@command -v swag >/dev/null 2>&1 || { \
		echo "$(WARN) Swagger CLI (swag) is not installed. Installing..."; \
		go install github.com/swaggo/swag/cmd/swag@latest; \
	}

reset:
	@echo "$(CLEAN) Performing complete project reset..."
	@rm -f $(BINARY_NAME)
	@rm -f $(CSS_OUTPUT)
	@rm -rf public/css/*
	@rm -f tailwind.config.js
	@rm -rf docs/docs.go docs/swagger.json docs/swagger.yaml
	@go clean -cache -testcache -modcache
	@rm -rf $(BASE_PATH)/tw  # Remove the 'tw' directory
	@echo "$(CHECK) Project reset complete"

clean:
	@echo "$(CLEAN) Cleaning build artifacts..."
	@rm -f $(BINARY_NAME)
	@rm -f $(CSS_OUTPUT)
	@rm -rf $(BASE_PATH)/lib/stylegen/tw  # Remove the 'tw' directory
	@echo "$(CHECK) Cleanup complete"

stylegen:
	@echo "$(CSS) Generating CSS styles..."
	@echo "Current working directory: $$(pwd)"
	@echo "Contents of current directory:"
	@ls -la
	@echo "\nContents of lib/stylegen:"
	@ls -la lib/stylegen
	@chmod +x $(BASE_PATH)/lib/stylegen/gen.sh
	@$(BASE_PATH)/lib/stylegen/gen.sh \
		-e "html" \
		-d "$(BASE_PATH)/pages/templates" \
		-o "$(BASE_PATH)/public/css"
	@echo "$(CHECK) CSS generation complete"

swaggergen: ensure-swag
	@echo "$(DOCS) Generating Swagger documentation..."
	@swag init
	@echo "$(CHECK) Swagger docs generated"

# Combined generation target
generate: stylegen swaggergen

build: generate
	@echo "$(BUILD) Building binary..."
	@go build -o $(BINARY_NAME)
	@echo "$(CHECK) Build complete: $(BINARY_NAME)"

test:
	@echo "$(TEST) Running tests..."
	@go test ./...
	@echo "$(CHECK) Tests complete"

dev: generate
	@echo "$(RUN) Starting development server..."
	@go run main.go

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

docker-dev:
	@echo "$(DOCKER) Starting development container..."
	@docker-compose -f docker-compose.dev.yml up --build

docker-build: generate
	@echo "$(DOCKER) Building production Docker image..."
	@docker build -t $(DOCKER_IMAGE) .
	@echo "$(CHECK) Docker image build complete"

docker-run: docker-build
	@echo "$(DOCKER) Running production container..."
	@docker-compose up

deps:
	@echo "$(BUILD) Installing dependencies..."
	@go mod download
	@go install github.com/swaggo/swag/cmd/swag@latest
	@echo "$(CHECK) Dependencies installed"

fmt:
	@echo "$(BUILD) Formatting code..."
	@go fmt ./...
	@echo "$(CHECK) Code formatting complete"

# Full production build
prod: clean generate build
	@echo "$(CHECK) Production build complete"

.DEFAULT_GOAL := help
