-include .env

PHP_CONTAINER ?= mtc-pharmacy-web
DB_CONTAINER ?= mariadb
PROD_PATH ?= ~/public_html

up:
	docker compose up -d

down:
	docker compose down

restart: down up

shell:
	docker exec -it $(PHP_CONTAINER) bash

logs:
	docker compose logs -f

dump-autoload:
	docker exec -it $(PHP_CONTAINER) composer dump-autoload

migrate:
	docker exec -it $(PHP_CONTAINER) php artisan migrate

fresh:
	docker exec -it $(PHP_CONTAINER) php artisan migrate:fresh --seed
	docker exec -it $(PHP_CONTAINER) php artisan encryption:run

seed:
	docker exec -it $(PHP_CONTAINER) php artisan db:seed

view-routes:
	docker exec -it $(PHP_CONTAINER) php artisan route:list

new-migration:
	docker exec -it $(PHP_CONTAINER) php artisan make:migration

new-controller:
	docker exec -it $(PHP_CONTAINER) php artisan make:controller

new-model:
	docker exec -it $(PHP_CONTAINER) php artisan make:model

queue:
	docker exec -it $(PHP_CONTAINER) php artisan queue:work

cache:
	docker exec -it $(PHP_CONTAINER) php artisan cache:clear
	docker exec -it $(PHP_CONTAINER) php artisan config:clear
	docker exec -it $(PHP_CONTAINER) php artisan route:clear
	docker exec -it $(PHP_CONTAINER) php artisan view:clear
	docker exec -it $(PHP_CONTAINER) php artisan optimize:clear

composer-install:
	docker exec -it $(PHP_CONTAINER) composer install

composer-update:
	docker exec -it $(PHP_CONTAINER) composer update

npm-install:
	docker exec -it $(PHP_CONTAINER) npm install

npm-dev:
	docker exec -it $(PHP_CONTAINER) npm run dev

npm-prod:
	docker exec -it $(PHP_CONTAINER) npm run build

npm-clean:
	docker exec -it $(PHP_CONTAINER) rm -rf node_modules
	docker exec -it $(PHP_CONTAINER) rm package-lock.json

npm-clear-cache:
	docker exec -it $(PHP_CONTAINER) rm -rf node_modules/.vite


db-sync: db-pull db-reset-admin
	@echo "Database sync complete!"

db-wipe:
	@echo "Wiping local database $(DB_DATABASE)..."
	docker exec -i $(DB_CONTAINER) mariadb -uroot -p$(DB_PASSWORD) -e \
		"DROP DATABASE IF EXISTS $(DB_DATABASE); CREATE DATABASE $(DB_DATABASE); \
		 GRANT ALL ON $(DB_DATABASE).* TO '$(DB_USERNAME)'@'%'; FLUSH PRIVILEGES;"
	@echo "Local database wiped."

db-wipe-sync: db-wipe db-pull db-reset-admin
	@echo "Database wipe + sync complete!"

ssh:
ifndef PROD_HOST
	$(error PROD_HOST is not set. Add PROD_HOST, PROD_USER to .env)
endif
	ssh -t $(PROD_USER)@$(PROD_HOST) "cd $(PROD_PATH) && exec \$$SHELL -l"

db-pull:
ifndef PROD_HOST
	$(error PROD_HOST is not set. Add PROD_HOST, PROD_USER, PROD_DB_NAME, PROD_DB_USER, PROD_DB_PASS to .env)
endif
	@echo "Pulling database from production..."
	@echo "This may take a few minutes depending on database size."
	ssh $(PROD_USER)@$(PROD_HOST) "mysqldump --no-tablespaces --skip-triggers -u $(PROD_DB_USER) -p'$(PROD_DB_PASS)' $(PROD_DB_NAME)" | sed 's/DEFINER=[^*]*\*/\*/g' | docker exec -i $(DB_CONTAINER) mariadb -u $(DB_USERNAME) -p$(DB_PASSWORD) $(DB_DATABASE)

db-reset-admin:
	@echo "Re-seeding admin user for local dev..."
	docker exec $(PHP_CONTAINER) php artisan db:seed --class=AdminUsersTableSeeder
	@echo "Running encryption to generate hashes..."
	docker exec $(PHP_CONTAINER) php artisan encryption:run

db-diff:
ifndef PROD_HOST
	$(error PROD_HOST is not set. Add PROD_HOST, PROD_USER, PROD_DB_NAME, PROD_DB_USER, PROD_DB_PASS to .env)
endif
	@echo "Comparing local vs production database schema..."
	@docker exec $(DB_CONTAINER) mariadb -u $(DB_USERNAME) -p$(DB_PASSWORD) $(DB_DATABASE) -N -e "SHOW TABLES" | sort > /tmp/local_tables.txt
	@ssh $(PROD_USER)@$(PROD_HOST) "mysql -u $(PROD_DB_USER) -p'$(PROD_DB_PASS)' $(PROD_DB_NAME) -N -e 'SHOW TABLES'" | sort > /tmp/prod_tables.txt
	@echo "=== Tables only in LOCAL ==="
	@comm -23 /tmp/local_tables.txt /tmp/prod_tables.txt || true
	@echo ""
	@echo "=== Tables only in PRODUCTION ==="
	@comm -13 /tmp/local_tables.txt /tmp/prod_tables.txt || true
	@rm -f /tmp/local_tables.txt /tmp/prod_tables.txt
