# Makefile for operating the make_post_sell server using either PyPI packages or source

# Variables (using the current working directory)
VENV_DIR = $(shell pwd)/env
DATA_DIR = $(shell pwd)/data
CONFIG_FILE = development.ini
# Using the provided configuration URL.
CONFIG_URL = https://git.unturf.com/engineering/make-post-sell/make_post_sell/-/raw/master/development.ini

PYTHON   = $(VENV_DIR)/bin/python
PIP      = $(VENV_DIR)/bin/pip
PSERVE   = $(VENV_DIR)/bin/pserve
ALEMBIC  = $(VENV_DIR)/bin/alembic
MPS_INIT = $(VENV_DIR)/bin/initialize_make_post_sell_db

# Default target: install from PyPI and then start the server.
all: install-from-pypi serve

# -----------------------------------------------------------------------------
# Environment Setup Targets
# -----------------------------------------------------------------------------

# Create virtual environment if not present.
$(VENV_DIR)/bin/activate:
	@echo "Creating virtual environment in $(VENV_DIR)..."
	python3 -m venv $(VENV_DIR)

venv: $(VENV_DIR)/bin/activate

# Create data directory and download configuration file if missing.
$(DATA_DIR)/$(CONFIG_FILE):
	@echo "Creating data directory in $(DATA_DIR) and downloading configuration file..."
	mkdir -p $(DATA_DIR)
	cd $(DATA_DIR) && wget -O $(CONFIG_FILE) $(CONFIG_URL)

config: $(DATA_DIR)/$(CONFIG_FILE)

# -----------------------------------------------------------------------------
# Package Installation Targets for PyPI Installation
# -----------------------------------------------------------------------------

# Install the make_post_sell core package from PyPI.
install-core: venv
	@echo "Installing make_post_sell core package from PyPI..."
	$(PIP) install make_post_sell

# Install development extras from PyPI.
install-dev: venv
	@echo "Installing make_post_sell development extras from PyPI..."
	$(PIP) install make_post_sell[dev]

# Combined PyPI installation target.
install: install-core install-dev

# -----------------------------------------------------------------------------
# Package Installation Targets for Source Installation
# -----------------------------------------------------------------------------

# Install from source (editable mode) for development and testing.
install-source-dev-and-test: venv
	@echo "Installing make_post_sell from source (editable mode) for development..."
	$(PIP) install --editable .
	$(PIP) install --upgrade -r requirements-dev.txt
	$(PIP) install --upgrade -r requirements-test.txt

# Install from source for production (non‑editable).
install-source-prod: venv
	@echo "Deleting tests from source code for production..."
	rm -rf make_post_sell/tests
	@echo "Installing make_post_sell from source for production (non‑editable)..."
	$(PIP) install .
	$(PIP) install --upgrade -r requirements-prod.txt

# -----------------------------------------------------------------------------
# Database Initialization and Server Targets
# -----------------------------------------------------------------------------

# Initialize the database using the configuration file.
init-db: venv config
	@echo "Initializing the make_post_sell database..."
	$(MPS_INIT) $(DATA_DIR)/$(CONFIG_FILE)
	$(ALEMBIC) -c $(DATA_DIR)/$(CONFIG_FILE) stamp head

# Start the development server with auto-reload.
serve: venv config
	@echo "Starting the make_post_sell development server..."
	$(PSERVE) $(DATA_DIR)/$(CONFIG_FILE) --reload

# -----------------------------------------------------------------------------
# Combined Setup Targets
# -----------------------------------------------------------------------------

# Install and set up using PyPI packages.
install-from-pypi: venv config install init-db

# Install and set up from source (editable mode) for development and testing.
install-from-source: venv config install-source-dev-and-test init-db

# Install and set up from source for production (non‑editable).
install-from-source-prod: venv config install-source-prod init-db

# -----------------------------------------------------------------------------
# Additional Targets
# -----------------------------------------------------------------------------

# Print instructions for activating the virtual environment.
activate:
	@echo "To activate the virtual environment, run:"
	@echo "  source $(VENV_DIR)/bin/activate"

# Run the test suite.
test: install-source-dev-and-test
	@echo "Running tests..."
	$(VENV_DIR)/bin/py.test

# Run tests with coverage for the full repository.
test-coverage: install-source-dev-and-test
	@echo "Running tests with coverage..."
	$(VENV_DIR)/bin/py.test --cov=make_post_sell --cov-report=term-missing --cov-report=html

# Start a simple HTTP server (if needed for static files).
http: venv
	@echo "Starting simple HTTP server on port 8000..."
	$(PYTHON) -m http.server 8000

# -----------------------------------------------------------------------------
# Cleanup Target
# -----------------------------------------------------------------------------

# Remove the virtual environment directories.
clean:
	@echo "Cleaning up: removing $(VENV_DIR)"
	rm -rf $(VENV_DIR)
