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

DC := docker compose

help:
	@echo ""
	@echo "  ╔════════════════════════════════════════════════╗"
	@echo "  ║  AvtoAmbor — auto parts inventory (dev tasks)  ║"
	@echo "  ╚════════════════════════════════════════════════╝"
	@echo ""
	@echo "  make install       Install npm dependencies inside the container"
	@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"
	@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 bundle-clean  Remove dist/"
	@echo ""

install:
	@$(DC) run --rm app npm install

run:
	@$(DC) up

build:
	@$(DC) run --rm app 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 && $(DC) run --rm app 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
	@$(DC) run --rm app 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

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