.PHONY: deploy install mirroring-github setup-github sync-all sync-from-github

# Variables de configuration
GITHUB_USER := scriptshadow
GITHUB_REPO := vaxmate-web
MAIN_BRANCH := main

# Variable d'environnement (par defaut: prod)
ENV ?= prod

# Configuration des options Composer selon l'environnement
ifeq ($(ENV),dev)
	COMPOSER_OPTIONS := --optimize-autoloader --ignore-platform-req=ext-sodium
	CACHE_ENV := dev
else
	COMPOSER_OPTIONS := --no-dev --optimize-autoloader --ignore-platform-req=ext-sodium
	CACHE_ENV := prod
endif

# Deployer le projet sur le serveur distant
deploy:
	@echo "============================================================"
	@echo "    DEBUT DU DEPLOIEMENT SUR LE SERVEUR (ENV: $(ENV))      "
	@echo "============================================================"
	ssh -A digest "\
	   cd public_html && \
	   git pull origin main && \
	   rm -rf var/cache/* && \
	   APP_ENV=$(CACHE_ENV) php composer.phar install $(COMPOSER_OPTIONS) && \
	   APP_ENV=$(CACHE_ENV) php bin/console cache:clear --env=$(CACHE_ENV) --no-debug"
	@echo "============================================================"
	@echo "     DEPLOIEMENT TERMINE AVEC SUCCES SUR LE SERVEUR !       "
	@echo "============================================================"

# Installer et configurer l'environnement local/distant
install: vendor/autoload.php
	@echo "INSTALLATION ET MISE A JOUR DES DEPENDANCES (ENV: $(ENV))"
	php bin/console cache:clear

# Installer les dependances Composer
vendor/autoload.php: composer.json composer.lock
	@echo "INSTALLATION DES DEPENDANCES COMPOSER (ENV: $(ENV))"
	php composer.phar install $(COMPOSER_OPTIONS)
	touch vendor/autoload.php

# Synchroniser avec GitHub (version simple)
mirroring-github:
	@echo "============================================================"
	@echo "        DEBUT DE LA SYNCHRONISATION VERS GITHUB            "
	@echo "============================================================"
	@if ! git remote | grep -q "github"; then \
		echo "[+] Ajout du remote GitHub..."; \
		git remote add github git@github.com:$(GITHUB_USER)/$(GITHUB_REPO).git; \
	fi
	@echo "[>>] Push vers GitHub..."
	@if git push github $(MAIN_BRANCH); then \
		echo "[OK] Synchronisation reussie !"; \
	else \
		echo "[ERROR] Erreur lors de la synchronisation"; \
		exit 1; \
	fi
	@echo "============================================================"
	@echo "    SYNCHRONISATION GITHUB TERMINEE AVEC SUCCES !          "
	@echo "============================================================"

# Synchroniser toutes les branches vers GitHub
mirroring-all-branches:
	@echo "============================================================"
	@echo "    SYNCHRONISATION COMPLETE VERS GITHUB (TOUTES BRANCHES) "
	@echo "============================================================"
	@if ! git remote | grep -q "github"; then \
		echo "[+] Ajout du remote GitHub..."; \
		git remote add github git@github.com:$(GITHUB_USER)/$(GITHUB_REPO).git; \
	fi
	@echo "[>>] Push de toutes les branches et tags..."
	@git push github --all
	@git push github --tags
	@echo "============================================================"
	@echo "  SYNCHRONISATION COMPLETE TERMINEE AVEC SUCCES !          "
	@echo "============================================================"

# Configuration initiale du remote GitHub
setup-github:
	@echo "============================================================"
	@echo "       CONFIGURATION INITIALE DU REMOTE GITHUB             "
	@echo "============================================================"
	@if [ "$(GITHUB_USER)" = "VOTRE_USERNAME" ] || [ "$(GITHUB_REPO)" = "VOTRE_REPO" ]; then \
		echo "[ERROR] Veuillez modifier GITHUB_USER et GITHUB_REPO dans le Makefile"; \
		exit 1; \
	fi
	@echo "[CONFIG] Configuration pour : $(GITHUB_USER)/$(GITHUB_REPO)"
	@if git remote | grep -q "github"; then \
		echo "[INFO] Remote GitHub deja configure :"; \
		git remote -v | grep github; \
	else \
		git remote add github git@github.com:$(GITHUB_USER)/$(GITHUB_REPO).git; \
		echo "[OK] Remote GitHub ajoute avec succes"; \
	fi
	@echo "[TEST] Test de connexion a GitHub..."
	@if git ls-remote github > /dev/null 2>&1; then \
		echo "[OK] Connexion GitHub OK"; \
	else \
		echo "[ERROR] Probleme de connexion GitHub - verifiez vos cles SSH"; \
		exit 1; \
	fi
	@echo "============================================================"

# Synchroniser depuis GitHub vers GitLab (verification et mise a jour)
sync-from-github:
	@echo "============================================================"
	@echo "    VERIFICATION ET SYNCHRONISATION DEPUIS GITHUB         "
	@echo "============================================================"
	@if ! git remote | grep -q "github"; then \
		echo "[ERROR] Remote GitHub non configure. Utilisez 'make setup-github' d'abord"; \
		exit 1; \
	fi
	@echo "[FETCH] Recuperation des dernieres informations depuis GitHub..."
	@git fetch github $(MAIN_BRANCH)
	@echo "[CHECK] Verification des differences..."
	@if git diff --quiet HEAD github/$(MAIN_BRANCH); then \
		echo "[OK] Aucune difference detectee - repositories synchronises"; \
	else \
		echo "[SYNC] Differences detectees entre local/GitLab et GitHub"; \
		echo "[PULL] Mise a jour depuis GitHub..."; \
		git pull github $(MAIN_BRANCH) --no-rebase || { \
			echo "[ERROR] Erreur lors du pull depuis GitHub"; \
			echo "[INFO] Resolution manuelle necessaire"; \
			echo "[HELP] Commandes suggerees :"; \
			echo "  git status"; \
			echo "  git pull github $(MAIN_BRANCH)"; \
			echo "  git push origin $(MAIN_BRANCH)"; \
			exit 1; \
		}; \
		echo "[PUSH] Push des changements vers GitLab..."; \
		git push origin $(MAIN_BRANCH); \
		echo "[OK] GitLab mis a jour avec les changements de GitHub"; \
	fi
	@echo "============================================================"
	@echo "    SYNCHRONISATION DEPUIS GITHUB TERMINEE !               "
	@echo "============================================================"

# Commande combinee : push vers GitLab ET GitHub
sync-all:
	@echo "============================================================"
	@echo "      SYNCHRONISATION VERS GITLAB ET GITHUB                "
	@echo "============================================================"
	@echo "[>>] Push vers GitLab..."
	@git push origin $(MAIN_BRANCH)
	@echo "[>>] Push vers GitHub..."
	@$(MAKE) mirroring-github
	@echo "============================================================"
	@echo "    SYNCHRONISATION COMPLETE TERMINEE !                    "
	@echo "============================================================"