# ITR Tracker Makefile
# Provides convenient commands for development and deployment

.PHONY: help dev build start test lint format clean install migrate seed backup

# Default target
help:
	@echo "ITR Tracker - Available Commands:"
	@echo ""
	@echo "Development:"
	@echo "  make install    - Install all dependencies"
	@echo "  make dev        - Start development servers (frontend + backend)"
	@echo "  make dev-backend - Start only backend server"
	@echo "  make dev-frontend - Start only frontend server"
	@echo ""
	@echo "Database:"
	@echo "  make migrate    - Run database migrations"
	@echo "  make seed       - Populate database with demo data"
	@echo "  make db-reset   - Reset database and reseed"
	@echo ""
	@echo "Code Quality:"
	@echo "  make format     - Format code (Python + JavaScript)"
	@echo "  make lint       - Run linters"
	@echo "  make test       - Run all tests"
	@echo "  make test-coverage - Run tests with coverage report"
	@echo ""
	@echo "Production:"
	@echo "  make build      - Build production assets"
	@echo "  make start      - Start production server"
	@echo "  make backup     - Backup database and files"
	@echo ""
	@echo "Utilities:"
	@echo "  make clean      - Clean build artifacts"
	@echo "  make logs       - Show application logs"

# Installation
install:
	@echo "Installing Python dependencies..."
	pip install -r server/requirements.txt
	@echo "Installing Node.js dependencies..."
	npm install
	@echo "Setting up pre-commit hooks..."
	pre-commit install

# Development
dev:
	@echo "Starting ITR Tracker in development mode..."
	npm run dev

dev-backend:
	@echo "Starting backend server..."
	cd server && python -m uvicorn index:app --reload --host 0.0.0.0 --port 8000

dev-frontend:
	@echo "Starting frontend server..."
	npm run dev:frontend

# Database
migrate:
	@echo "Running database migrations..."
	npm run db:push

seed:
	@echo "Seeding database with demo data..."
	python server/seed_data.py

db-reset:
	@echo "Resetting database..."
	@echo "⚠️  This will delete all data. Press Ctrl+C to cancel, or Enter to continue..."
	@read
	rm -f *.db
	make migrate
	make seed

# Code Quality
format:
	@echo "Formatting Python code..."
	black server/ --line-length 88 --target-version py38
	isort server/ --profile black
	@echo "Formatting JavaScript/TypeScript code..."
	npm run format

lint:
	@echo "Linting Python code..."
	ruff server/
	@echo "Linting JavaScript/TypeScript code..."
	npm run lint

test:
	@echo "Running Python tests..."
	cd server && python -m pytest tests/ -v
	@echo "Running JavaScript tests..."
	npm run test

test-coverage:
	@echo "Running tests with coverage..."
	cd server && python -m pytest tests/ --cov=. --cov-report=html --cov-report=term
	@echo "Coverage report generated in server/htmlcov/"

# Production
build:
	@echo "Building production assets..."
	npm run build

start:
	@echo "Starting production server..."
	npm run start

# Utilities
clean:
	@echo "Cleaning build artifacts..."
	rm -rf dist/
	rm -rf server/htmlcov/
	rm -rf server/.pytest_cache/
	rm -rf node_modules/.cache/
	find . -type d -name __pycache__ -exec rm -rf {} +
	find . -type f -name "*.pyc" -delete

backup:
	@echo "Creating backup..."
	mkdir -p backups
	@echo "Backing up database..."
	cp *.db backups/database_$(shell date +%Y%m%d_%H%M%S).db 2>/dev/null || true
	@echo "Backing up uploads..."
	tar -czf backups/uploads_$(shell date +%Y%m%d_%H%M%S).tar.gz uploads/ 2>/dev/null || true
	@echo "Backup completed in backups/ directory"

logs:
	@echo "Showing application logs..."
	tail -f logs/app.log 2>/dev/null || echo "No log file found. Start the application first."

# Environment setup
setup-env:
	@echo "Setting up environment..."
	@if [ ! -f .env ]; then \
		echo "Creating .env file from template..."; \
		cp .env.example .env; \
		echo "Please edit .env file with your configuration"; \
	else \
		echo ".env file already exists"; \
	fi

# Docker commands (for future use)
docker-build:
	@echo "Building Docker image..."
	docker build -t itr-tracker .

docker-run:
	@echo "Running Docker container..."
	docker run -p 5000:5000 --env-file .env itr-tracker

# Compliance and audit
audit:
	@echo "Running security audit..."
	npm audit
	pip-audit

compliance-report:
	@echo "Generating compliance report..."
	python scripts/compliance_report.py

# Quick setup for new environments
quick-start: setup-env install migrate seed
	@echo ""
	@echo "🎉 ITR Tracker setup complete!"
	@echo ""
	@echo "To start development:"
	@echo "  make dev"
	@echo ""
	@echo "Demo login credentials:"
	@echo "  Partner: admin@ca-firm.com / admin123"
	@echo "  Manager: manager@ca-firm.com / manager123"
	@echo "  Staff: staff@ca-firm.com / staff123"
	@echo ""
	@echo "Access the application at: http://localhost:5000"
