.PHONY: help install-backend install-frontend dev-backend dev-frontend dev migrate-up migrate-down migrate-create setup clean seed

# Default target
.DEFAULT_GOAL := help

help: ## Show this help message
	@echo 'Usage: make [target]'
	@echo ''
	@echo 'Available targets:'
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "  %-20s %s\n", $$1, $$2}' $(MAKEFILE_LIST)

# Installation targets
install-backend: ## Install backend dependencies
	@echo "Installing backend dependencies..."
	cd backend && uv sync

FRONTEND_MANAGER ?= bun

install-frontend: ## Install frontend dependencies
	@echo "Installing frontend dependencies..."
	cd frontend && $(FRONTEND_MANAGER) install

install: install-backend install-frontend ## Install all dependencies

# Development server targets
dev-backend: ## Run backend development server
	@echo "Starting backend server..."
	cd backend && uv run fastapi dev app/main.py

dev-frontend: ## Run frontend development server
	@echo "Starting frontend server..."
	cd frontend && $(FRONTEND_MANAGER) run dev

dev: ## Run both backend and frontend servers concurrently
	@echo "Starting both backend and frontend servers..."
	@make -j2 dev-backend dev-frontend

# Migration targets
migrate-create: ## Create a new migration (usage: make migrate-create NAME=migration_name)
	@if [ -z "$(NAME)" ]; then \
		echo "Error: NAME is required. Usage: make migrate-create NAME=migration_name"; \
		exit 1; \
	fi
	@echo "Creating migration: $(NAME)..."
	cd backend && uv run alembic revision --autogenerate -m "$(NAME)"

migrate-up: ## Apply all pending migrations
	@echo "Applying migrations..."
	cd backend && uv run alembic upgrade head

migrate-down: ## Rollback last migration
	@echo "Rolling back last migration..."
	cd backend && uv run alembic downgrade -1

migrate-history: ## Show migration history
	@echo "Migration history:"
	cd backend && uv run alembic history

migrate-current: ## Show current migration version
	@echo "Current migration version:"
	cd backend && uv run alembic current

# Setup target
setup: install ## Install dependencies and setup project
	@echo "Setup complete! Don't forget to:"
	@echo "  1. Configure backend/.env.development with your DATABASE_URL"
	@echo "  2. Configure frontend/.env.development with your VITE_API_URL and VITE_GOOGLE_MAPS_API_KEY"
	@echo "  3. Run 'make migrate-up' to apply database migrations"

# Build targets
build-backend: ## Build backend for production
	@echo "Building backend..."
	cd backend && uv build

build-frontend: ## Build frontend for production
	@echo "Building frontend..."
	cd frontend && $(FRONTEND_MANAGER) run build

build: build-backend build-frontend ## Build both backend and frontend

# Production server targets
run-backend: ## Run backend production server
	@echo "Starting backend production server..."
	cd backend && uv run fastapi run app/main.py

# Utility targets
clean-backend: ## Clean backend cache and temporary files
	@echo "Cleaning backend..."
	cd backend && find . -type d -name __pycache__ -exec rm -r {} + 2>/dev/null || true
	cd backend && find . -type f -name "*.pyc" -delete
	cd backend && rm -rf .pytest_cache .mypy_cache .coverage htmlcov 2>/dev/null || true

clean-frontend: ## Clean frontend build artifacts
	@echo "Cleaning frontend..."
	cd frontend && rm -rf dist node_modules/.vite 2>/dev/null || true

clean: clean-backend clean-frontend ## Clean all build artifacts

# Database targets
db-init: ## Initialize database (create tables)
	@echo "Initializing database..."
	cd backend && uv run python -c "from app.core.database import init_db; init_db()"

db-reset: ## Reset database (drop all tables and recreate)
	@echo "Resetting database..."
	@echo "⚠️  This will drop all tables and data!"
	@read -p "Are you sure? [y/N] " -n 1 -r; \
	echo; \
	if [[ $$REPLY =~ ^[Yy]$$ ]]; then \
		cd backend && uv run python -c "from sqlalchemy import text; from app.core.database import engine; from sqlmodel import Session; session = Session(engine); session.exec(text('DROP SCHEMA IF EXISTS public CASCADE; CREATE SCHEMA public;')); session.commit(); session.close()"; \
		echo "Database reset complete. Run 'make migrate-up' to recreate tables."; \
	else \
		echo "Cancelled."; \
	fi

db-reset-alembic: ## Reset alembic version table (use when rebuilding migrations)
	@echo "Resetting alembic version table..."
	cd backend && uv run python -c "from sqlalchemy import text; from app.core.database import engine; from sqlmodel import Session; session = Session(engine); session.exec(text('DROP TABLE IF EXISTS alembic_version;')); session.commit(); session.close()"
	@echo "Alembic version table reset. You can now create a new migration."

seed: ## Seed database with initial data
	@echo "Seeding database..."
	cd backend && uv run python -m app.core.seed

import-coordinates: ## Import bus stop coordinates from Supabase (usage: make import-coordinates ROUTE="Boquete>David" or make import-coordinates ALL=true)
	@echo "Importing coordinates from Supabase..."
	@if [ "$(ALL)" = "true" ]; then \
		cd backend && uv run python -m app.core.import_supabase_coordinates --all; \
	else \
		cd backend && uv run python -m app.core.import_supabase_coordinates "$(ROUTE)"; \
	fi
##Probar los lint
# Format and lint targets
format-backend: ## Format backend code
	@echo "Formatting backend code..."
	cd backend && uv run ruff format .

lint-backend: install-backend ## Lint backend code
	@echo "Linting backend code..."
	cd backend && uv run ruff check . --fix

format-frontend: ## Format frontend code
	@echo "Formatting frontend code..."
	cd frontend && $(FRONTEND_MANAGER) run format || echo "No format script found"

lint-frontend: install-frontend ## Lint frontend code
	@echo "Linting frontend code..."
	cd frontend && $(FRONTEND_MANAGER) run build
# 	cd frontend && $(FRONTEND_MANAGER) run lint || echo "No lint script found"

lint: lint-backend lint-frontend ## Lint all code

# Test targets
test-backend: ## Run backend tests
	@echo "Running backend tests..."
	cd backend && uv run pytest || echo "No tests found"

test-frontend: ## Run frontend tests
	@echo "Running frontend tests..."
	cd frontend && $(FRONTEND_MANAGER) run test || echo "No tests found"

test: test-backend test-frontend ## Run all tests

