.PHONY: build clean test lint lintfix run-server run-cli help build-ui migrate generate-models install-relspecgo seed

# Variables
FRONTEND_DIR=web
SQL_DIR=sql
MODELS_DIR=pkg/models

# Build both server and CLI
build: build-server build-cli build-ui

build-all: build ## Alias for build

# Build server and CLI
build-backend:
	@echo "Building WhatsHooked backend..."
	@mkdir -p bin
	@go build -o bin/whatshook-server ./cmd/server
	@go build -o bin/whatshook-cli ./cmd/cli
	@echo "Backend build complete! Binaries in bin/"

# Build server only
build-server:
	@echo "Building server..."
	@mkdir -p bin
	@go build -o bin/whatshook-server ./cmd/server
	@echo "Server built: bin/whatshook-server"

# Build CLI only
build-cli:
	@echo "Building CLI..."
	@mkdir -p bin
	@go build -o bin/whatshook-cli ./cmd/cli
	@echo "CLI built: bin/whatshook-cli"

# Clean build artifacts (preserves bin directory)
clean:
	@echo "Cleaning..."
	@mkdir -p bin
	@rm -f bin/whatshook*
	@echo "Clean complete!"

# Run tests with coverage
test:
	@echo "Running tests..."
	@go test -v -coverprofile=coverage.out -covermode=atomic ./...
	@echo "Coverage report saved to coverage.out"

# Run server (requires config.json)
run-server:
	@go run ./cmd/server -config config.json

# Run CLI
run-cli:
	@go run ./cmd/cli $(ARGS)

# Install dependencies
deps:
	@echo "Installing dependencies..."
	@go mod download
	@go mod tidy
	@if [ -d "$(FRONTEND_DIR)" ]; then \
		echo "Installing frontend dependencies..."; \
		cd $(FRONTEND_DIR) && npm install; \
	fi
	@echo "Dependencies installed!"

# Build frontend UI
build-ui:
	@echo "Building frontend..."
	@if [ -d "$(FRONTEND_DIR)" ]; then \
		cd $(FRONTEND_DIR) && npm run build; \
		echo "Frontend built successfully"; \
	else \
		echo "Frontend directory not found. Skipping..."; \
	fi

# Development mode for frontend
dev-ui:
	@echo "Starting frontend development server..."
	@if [ -d "$(FRONTEND_DIR)" ]; then \
		cd $(FRONTEND_DIR) && npm run dev; \
	else \
		echo "Frontend directory not found"; \
		exit 1; \
	fi

# Database commands
generate-models: ## Generate BUN models from DBML schema
	@echo "Generating models from DBML..."
	@if [ ! -f "$(SQL_DIR)/schema.dbml" ]; then \
		echo "Error: $(SQL_DIR)/schema.dbml not found"; \
		echo "Please create the DBML schema first"; \
		exit 1; \
	fi
	@mkdir -p $(MODELS_DIR)
	@$(HOME)/go/bin/relspec convert --from dbml --from-path=$(SQL_DIR)/schema.dbml --to bun --to-path=$(MODELS_DIR) --package models
	@echo "Models generated in $(MODELS_DIR)"

migrate: migrate-up ## Alias for migrate-up

migrate-up: ## Run database migrations (up)
	@echo "Running migrations..."
	@if [ -f "$(SQL_DIR)/migrate.sh" ]; then \
		bash $(SQL_DIR)/migrate.sh up; \
	else \
		echo "Migration script not found. Run 'make setup-migrations' first"; \
	fi

migrate-down: ## Rollback database migrations (down)
	@echo "Rolling back migrations..."
	@if [ -f "$(SQL_DIR)/migrate.sh" ]; then \
		bash $(SQL_DIR)/migrate.sh down; \
	else \
		echo "Migration script not found"; \
	fi

migrate-create: ## Create a new migration file (usage: make migrate-create NAME=migration_name)
	@if [ -z "$(NAME)" ]; then \
		echo "Error: NAME is required. Usage: make migrate-create NAME=migration_name"; \
		exit 1; \
	fi
	@mkdir -p $(SQL_DIR)/postgres $(SQL_DIR)/sqlite
	@timestamp=$$(date +%Y%m%d%H%M%S); \
	echo "-- Migration: $(NAME)" > $(SQL_DIR)/postgres/$${timestamp}_$(NAME).up.sql; \
	echo "-- Rollback: $(NAME)" > $(SQL_DIR)/postgres/$${timestamp}_$(NAME).down.sql; \
	echo "-- Migration: $(NAME)" > $(SQL_DIR)/sqlite/$${timestamp}_$(NAME).up.sql; \
	echo "-- Rollback: $(NAME)" > $(SQL_DIR)/sqlite/$${timestamp}_$(NAME).down.sql; \
	echo "Migration files created in $(SQL_DIR)/postgres and $(SQL_DIR)/sqlite"

setup-migrations: ## Setup migration scripts
	@echo "Setting up migration infrastructure..."
	@mkdir -p $(SQL_DIR)/postgres $(SQL_DIR)/sqlite
	@if [ ! -f "$(SQL_DIR)/migrate.sh" ]; then \
		echo "Creating migration script..."; \
		echo '#!/bin/bash' > $(SQL_DIR)/migrate.sh; \
		echo 'echo "Migration script placeholder. Implement your migration logic here."' >> $(SQL_DIR)/migrate.sh; \
		chmod +x $(SQL_DIR)/migrate.sh; \
	fi
	@echo "Migration infrastructure ready"

# Seed database
seed: ## Seed the database with initial data
	@echo "Seeding database..."
	@go run ./cmd/seed

# Install tools
install-relspecgo: ## Install relspecgo tool
	@echo "Installing relspec..."
	@go install git.warky.dev/wdevs/relspecgo/cmd/relspec@latest
	@echo "relspec installed successfully"

install-tools: install-relspecgo ## Install all required tools
	@echo "Installing development tools..."
	@go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
	@echo "All tools installed"


release-version: ## Create and push a release with specific version (use: make release-version VERSION=v1.2.3)
	@if [ -z "$(VERSION)" ]; then \
		echo "Error: VERSION is required. Usage: make release-version VERSION=v1.2.3"; \
		exit 1; \
	fi
	@version="$(VERSION)"; \
	if ! echo "$$version" | grep -q "^v"; then \
		version="v$$version"; \
	fi; \
	echo "Creating release: $$version"; \
	latest_tag=$$(git describe --tags --abbrev=0 2>/dev/null || echo ""); \
	if [ -z "$$latest_tag" ]; then \
		commit_logs=$$(git log --pretty=format:"- %s" --no-merges); \
	else \
		commit_logs=$$(git log "$${latest_tag}..HEAD" --pretty=format:"- %s" --no-merges); \
	fi; \
	if [ -z "$$commit_logs" ]; then \
		tag_message="Release $$version"; \
	else \
		tag_message="Release $$version\n\n$$commit_logs"; \
	fi; \
	git tag -a "$$version" -m "$$tag_message"; \
	git push origin "$$version"; \
	echo "Tag $$version created and pushed to remote repository."


lint: ## Run linter
	@echo "Running linter..."
	@if command -v golangci-lint > /dev/null; then \
		golangci-lint run --config=.golangci.json; \
	else \
		echo "golangci-lint not installed. Install with: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest"; \
		exit 1; \
	fi

lintfix: ## Run linter
	@echo "Running linter..."
	@if command -v golangci-lint > /dev/null; then \
		golangci-lint run --config=.golangci.json --fix; \
	else \
		echo "golangci-lint not installed. Install with: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest"; \
		exit 1; \
	fi

# Help
help:
	@echo "WhatsHooked Makefile"
	@echo ""
	@echo "Build Commands:"
	@echo "  make build              - Build server, CLI, and UI"
	@echo "  make build-backend      - Build server and CLI only"
	@echo "  make build-server       - Build server only"
	@echo "  make build-cli          - Build CLI only"
	@echo "  make build-ui           - Build frontend UI"
	@echo ""
	@echo "Development Commands:"
	@echo "  make run-server         - Run server (requires config.json)"
	@echo "  make run-cli ARGS='...' - Run CLI with arguments"
	@echo "  make dev-ui             - Run frontend in development mode"
	@echo ""
	@echo "Database Commands:"
	@echo "  make generate-models    - Generate BUN models from DBML schema"
	@echo "  make migrate            - Run database migrations (up)"
	@echo "  make migrate-up         - Run database migrations (up)"
	@echo "  make migrate-down       - Rollback database migrations"
	@echo "  make migrate-create NAME=<name> - Create new migration"
	@echo "  make setup-migrations   - Setup migration infrastructure"
	@echo "  make seed               - Seed database with initial data"
	@echo ""
	@echo "Test & Quality:"
	@echo "  make test               - Run tests with coverage"
	@echo "  make lint               - Run linter"
	@echo "  make lintfix            - Run linter with auto-fix"
	@echo ""
	@echo "Dependencies:"
	@echo "  make deps               - Install all dependencies"
	@echo "  make install-relspecgo  - Install relspecgo tool"
	@echo "  make install-tools      - Install all development tools"
	@echo ""
	@echo "Other:"
	@echo "  make clean              - Remove build artifacts"
	@echo "  make release-version VERSION=v1.2.3 - Create release"
	@echo "  make help               - Show this help message"

