# Familiar Makefile
# Common commands for setup and operation

.PHONY: install init run telegram discord dashboard gateway node test clean help

# Default target
help:
	@echo "Familiar - Available Commands"
	@echo ""
	@echo "Setup:"
	@echo "  make install    - Install Python dependencies"
	@echo "  make init       - Initialize configuration"
	@echo "  make install-pi - Full Raspberry Pi setup"
	@echo ""
	@echo "Run:"
	@echo "  make run        - Run in CLI mode"
	@echo "  make telegram   - Run Telegram bot"
	@echo "  make discord    - Run Discord bot"
	@echo "  make dashboard  - Run web dashboard"
	@echo "  make gateway    - Run as mesh gateway"
	@echo "  make node       - Run as mesh node"
	@echo "  make all        - Run gateway + dashboard + telegram"
	@echo ""
	@echo "Development:"
	@echo "  make test       - Run tests"
	@echo "  make lint       - Check code style"
	@echo "  make clean      - Remove cache files"
	@echo "  make plugin     - Create new plugin template"
	@echo ""
	@echo "Service:"
	@echo "  make service-install   - Install systemd service"
	@echo "  make service-start     - Start service"
	@echo "  make service-stop      - Stop service"
	@echo "  make service-logs      - View service logs"

# Setup
install:
	pip install -r requirements.txt --break-system-packages

init:
	python -m familiar --init

install-pi: install
	pip install playwright --break-system-packages
	playwright install chromium
	sudo apt install -y espeak portaudio19-dev
	pip install pyttsx3 SpeechRecognition pyaudio --break-system-packages
	python -m familiar --init
	@echo ""
	@echo "✓ Familiar installed!"
	@echo "  Set your API key: export ANTHROPIC_API_KEY=..."
	@echo "  Then run: make telegram"

# Run modes
run:
	python -m familiar

telegram:
	python -m familiar --telegram

discord:
	python -m familiar --discord

dashboard:
	python -m familiar --dashboard

gateway:
	python -m familiar --gateway

node:
	@if [ -z "$(GATEWAY)" ]; then \
		echo "Usage: make node GATEWAY=ws://pi.local:18789 KEY=your-key"; \
		exit 1; \
	fi
	python -m familiar.node --gateway $(GATEWAY) --key $(KEY)

all:
	python -m familiar --gateway --dashboard --telegram

# Development
test:
	python -m pytest tests/ -v

test-cov:
	python -m pytest tests/ -v --cov=familiar --cov-report=term-missing --cov-report=html

test-fast:
	python -m pytest tests/ -v -x --tb=short

test-unit:
	python -m pytest tests/ -v -m "not integration"

test-watch:
	python -m pytest tests/ -v --tb=short -f

lint:
	python -m flake8 familiar/ --max-line-length=120

typecheck:
	python -m mypy familiar/core/ --ignore-missing-imports

clean:
	find . -type d -name __pycache__ -exec rm -rf {} +
	find . -type f -name "*.pyc" -delete
	find . -type f -name ".DS_Store" -delete

plugin:
	@if [ -z "$(NAME)" ]; then \
		echo "Usage: make plugin NAME=my-plugin"; \
		exit 1; \
	fi
	python -m familiar.create_plugin $(NAME)

# Systemd service
SERVICE_FILE=/etc/systemd/system/familiar.service

service-install:
	@echo "[Unit]" | sudo tee $(SERVICE_FILE)
	@echo "Description=Familiar" | sudo tee -a $(SERVICE_FILE)
	@echo "After=network.target" | sudo tee -a $(SERVICE_FILE)
	@echo "" | sudo tee -a $(SERVICE_FILE)
	@echo "[Service]" | sudo tee -a $(SERVICE_FILE)
	@echo "Type=simple" | sudo tee -a $(SERVICE_FILE)
	@echo "User=$(USER)" | sudo tee -a $(SERVICE_FILE)
	@echo "WorkingDirectory=$(PWD)" | sudo tee -a $(SERVICE_FILE)
	@echo "EnvironmentFile=$(HOME)/.familiar/.env" | sudo tee -a $(SERVICE_FILE)
	@echo "ExecStart=/usr/bin/python3 -m familiar --telegram --gateway" | sudo tee -a $(SERVICE_FILE)
	@echo "Restart=always" | sudo tee -a $(SERVICE_FILE)
	@echo "RestartSec=10" | sudo tee -a $(SERVICE_FILE)
	@echo "" | sudo tee -a $(SERVICE_FILE)
	@echo "[Install]" | sudo tee -a $(SERVICE_FILE)
	@echo "WantedBy=multi-user.target" | sudo tee -a $(SERVICE_FILE)
	sudo systemctl daemon-reload
	sudo systemctl enable familiar
	@echo ""
	@echo "✓ Service installed!"
	@echo "  Create ~/.familiar/.env with your API keys"
	@echo "  Then: make service-start"

service-start:
	sudo systemctl start familiar

service-stop:
	sudo systemctl stop familiar

service-restart:
	sudo systemctl restart familiar

service-status:
	sudo systemctl status familiar

service-logs:
	journalctl -u familiar -f

# Show secret key
show-key:
	python -m familiar --show-key
