## ──────────────────────────────────────────────
##  WGSecure (WGS) — Makefile
##  Compilation Linux & Windows via PyInstaller
## ──────────────────────────────────────────────

APP     := wgsecure
VERSION := 0.4.0
PYTHON  := python3
SRC     := main.py
DIST    := dist
BUILD   := build
ICON    := /tmp/wgsecure_icon_build.png

# Options PyInstaller communes aux deux plateformes
PI_OPTS := \
	--noconfirm \
	--clean \
	--name $(APP) \
	--hidden-import pyotp \
	--hidden-import qrcode \
	--hidden-import qrcode.image.pil \
	--hidden-import PIL \
	--hidden-import PIL.Image \
	--hidden-import cryptography \
	--hidden-import cryptography.hazmat.primitives.asymmetric.x25519 \
	--collect-submodules PyQt6

.PHONY: all linux windows release install run run-admin icon clean help

# ── Cible par défaut ────────────────────────────────────────────────────────
all: linux

# ── Génération de l'icône PNG (utilisée par PyInstaller) ────────────────────
icon:
	@$(PYTHON) -c "\
import sys; sys.path.insert(0,'.');\
from PyQt6.QtWidgets import QApplication; app=QApplication(sys.argv);\
from app.ui.icons import icon_app;\
pix=icon_app().pixmap(256,256); pix.save('$(ICON)','PNG');\
print('  Icône générée →', '$(ICON)')" 2>/dev/null
	@test -f $(ICON) || (echo "  ❌  Génération icône échouée"; exit 1)

# ── Binaire Linux ────────────────────────────────────────────────────────────
linux: icon
	@echo ""
	@echo "  🐧  Compilation Linux (PyInstaller onefile)…"
	@echo ""
	$(PYTHON) -m PyInstaller $(PI_OPTS) \
		--onefile \
		--icon=$(ICON) \
		$(SRC)
	@echo ""
	@echo "  ✅  Binaire Linux → $(DIST)/$(APP)"
	@echo ""

# ── Binaire Windows (via Wine + Python Windows) ──────────────────────────────
windows: icon
	@echo ""
	@echo "  🪟  Compilation Windows (Wine + PyInstaller)…"
	@echo ""
	@which wine > /dev/null 2>&1 \
		|| (echo "  ❌  Wine non installé → sudo apt install wine"; exit 1)
	@wine python -m PyInstaller $(PI_OPTS) \
		--onefile \
		--windowed \
		--icon=$(ICON) \
		$(SRC) \
	|| (echo ""; \
	    echo "  ❌  Échec : Python Windows introuvable dans Wine."; \
	    echo "  → Installez-le avec :  make setup-wine"; \
	    echo ""; exit 1)
	@echo ""
	@echo "  ✅  Binaire Windows → $(DIST)/$(APP).exe"
	@echo ""

# ── Installation de Python dans Wine ─────────────────────────────────────────
setup-wine:
	@echo "  📦  Installation Python + pip dans Wine…"
	winetricks python3
	wine pip install -r requirements.txt
	wine pip install pyinstaller

# ── Release (Linux renommé avec version) ─────────────────────────────────────
release: clean linux
	@mv $(DIST)/$(APP) $(DIST)/$(APP)-$(VERSION)-linux-x86_64
	@echo "  📦  Release → $(DIST)/$(APP)-$(VERSION)-linux-x86_64"

# ── Dépendances Python ────────────────────────────────────────────────────────
install:
	$(PYTHON) -m pip install -r requirements.txt
	$(PYTHON) -m pip install pyinstaller
	@echo "  ✅  Dépendances installées"

# ── Lancer l'application ─────────────────────────────────────────────────────
run:
	DISPLAY=:0 $(PYTHON) $(SRC)

run-admin:
	DISPLAY=:0 $(PYTHON) $(SRC) --admin

# ── Nettoyage ────────────────────────────────────────────────────────────────
clean:
	@echo "  🧹  Nettoyage…"
	@rm -rf $(BUILD) $(DIST) *.spec
	@find . -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null; true
	@find . -name "*.pyc" -delete 2>/dev/null; true
	@echo "  ✅  Artefacts supprimés"

# ── Aide ─────────────────────────────────────────────────────────────────────
help:
	@echo ""
	@echo "  ╔════════════════════════════════════════════╗"
	@echo "  ║  🛡️  WGSecure v$(VERSION) — Makefile           ║"
	@echo "  ╠════════════════════════════════════════════╣"
	@echo "  ║  make install      Installe les dépendances ║"
	@echo "  ║  make linux        Binaire Linux (onefile)  ║"
	@echo "  ║  make windows      Binaire Windows (Wine)   ║"
	@echo "  ║  make release      Linux + nommage release  ║"
	@echo "  ║  make setup-wine   Python Windows dans Wine ║"
	@echo "  ║  make run          Lance l'application      ║"
	@echo "  ║  make run-admin    Lance en mode admin      ║"
	@echo "  ║  make clean        Supprime les artefacts   ║"
	@echo "  ╚════════════════════════════════════════════╝"
	@echo ""
