.PHONY: help install run build db-init db-reset docker-build docker-shell clean clean-all bundle patch bundle-clean

SHELL := /bin/bash
# Activate the Node version pinned in .nvmrc (16.x) for every recipe.
NVM := . $$HOME/.nvm/nvm.sh && nvm use --silent

DC := docker compose

help:
	@echo ""
	@echo "  ╔════════════════════════════════════════════════╗"
	@echo "  ║  AvtoAmbor — auto parts inventory (dev tasks)  ║"
	@echo "  ╚════════════════════════════════════════════════╝"
	@echo ""
	@echo "  make install       Install npm dependencies (Node 16 via nvm)"
	@echo "  make run           Start the dev server (http://localhost:3000)"
	@echo "  make build         Production build into ./build (adapter-node)"
	@echo "  make db-init       Create data/avtoambor.db from schema + seed (skip if exists)"
	@echo "  make db-reset      DELETE and recreate data/avtoambor.db (asks first)"
	@echo "  make docker-build  Rebuild the Docker image (legacy; dev runs on host now)"
	@echo "  make docker-shell  Open an interactive bash shell in the container"
	@echo "  make clean         Remove node_modules and build/ (keeps data/)"
	@echo "  make clean-all     Also wipe data/ (destroys the DB)"
	@echo "  make bundle        Produce dist/avtoambor-deploy.zip for Windows 7"
	@echo "  make patch         Produce dist/avtoambor-patch.zip (build/ only) for an installed target"
	@echo "  make bundle-clean  Remove dist/"
	@echo ""

install:
	@$(NVM) && npm install

run:
	@$(NVM) && npm run dev

build:
	@$(NVM) && npm run build

db-init:
	@if [ -f data/avtoambor.db ]; then \
		echo "data/avtoambor.db already exists — skipping. Use 'make db-reset' to recreate."; \
	else \
		mkdir -p data && $(NVM) && node scripts/init-db.js; \
	fi

db-reset:
	@printf "This will DELETE data/avtoambor.db. Continue? [y/N] " && read ans && [ "$$ans" = "y" ] || (echo "aborted." && exit 1)
	@rm -f data/avtoambor.db data/avtoambor.db-shm data/avtoambor.db-wal
	@mkdir -p data
	@$(NVM) && node scripts/init-db.js

docker-build:
	@$(DC) build

docker-shell:
	@$(DC) run --rm app bash

clean:
	@rm -rf node_modules build .svelte-kit
	@echo "removed node_modules, build/, .svelte-kit/  (data/ kept)"

clean-all: clean
	@rm -rf data
	@echo "wiped data/ as well."

# Windows 7 deploy bundle. Must be run with Node 16 active (.nvmrc).
bundle:
	@bash scripts/make-bundle.sh

# Small build/-only update zip for an already-installed target.
# Requires a prior `make bundle` to establish dist/avtoambor/ as the baseline.
patch:
	@bash scripts/make-patch.sh

bundle-clean:
	@rm -rf dist
	@echo "removed dist/"
