#!/usr/bin/env just --justfile

alias t := test
alias c := check-typing
alias l := lint
alias lf := lint-fix
alias f := format
alias s := start-services
alias d := dev
alias w := worker-start
alias tc := test-cov

# ✅ Run all tests
test:
    uv run pytest

# 📊 Run tests with coverage report
test-cov:
    uv run pytest --cov=issue_solver --cov-report=term --cov-report=html

# 🧹 Lint the code
lint:
    uv run ruff check

# ✨ Fix linting issues
lint-fix:
    uv run ruff check --fix

# 🎨 Format the code
format:
    uv run ruff format

# ❇️ Check typing with mypy
check-typing:
    uv run mypy src tests --disable-error-code=import-untyped

set dotenv-load := true

# 🚀 Start the API
api-start:
    uv run fastapi dev src/issue_solver/webapi/main.py

# ▶️ Run cli issue solver
run:
    uv run cudu help

# 🧩 Run cudu cli to solve issue
solve:
    uv run cudu solve

# 🧩 Run cudu cli to solve issue with sudo (for privileged operations like  swe-agent in docker)
solve-priveleged:
    @echo "Running cudu solve with sudo..."
    sudo uv run cudu solve

# 🐳 Build WebAPI Container image 🐳🌐📦
build-webapi-container:
    DOCKER_BUILDKIT=1 docker build -t umans-platform-webapi -f webapi.Dockerfile .

# 🐳 Build Worker Container image 🐳👷‍♂️📦
build-worker-container:
    DOCKER_BUILDKIT=1 docker build -t umans-platform-worker -f worker.Dockerfile .

# 🔌 Start LocalStack and other backing services
start-services:
    @echo "Starting LocalStack and other backing services..."
    docker-compose up -d
    @echo "Waiting for LocalStack to be ready..."
    sleep 2
    @echo "Checking if LocalStack is accessible..."
    curl -s http://localhost:4566/_localstack/health || (echo "LocalStack is not accessible. Check docker logs with 'docker logs issue-solver-localstack'" && exit 1)
    @echo "LocalStack is ready!"

# 🛑 Stop LocalStack and other backing services
stop-services:
    @echo "Stopping LocalStack and other backing services..."
    docker-compose down

# ❌ Destroy LocalStack and other backing services. This will remove all data, containers, and volumes.
destroy-services:
    @echo "Destroying LocalStack and other backing services..."
    docker-compose down -v

# 📋 Get SQS queue URL
get-queue-url:
    @echo "Getting SQS queue URL..."
    aws --endpoint-url=http://localhost:4566 sqs get-queue-url --queue-name process-queue

# 📝 List SQS queues
list-queues:
    @echo "Listing SQS queues..."
    aws --endpoint-url=http://localhost:4566 sqs list-queues

# 📬 Send test message to SQS queue
send-test-message:
    @echo "Sending test message to SQS queue..."
    aws --endpoint-url=http://localhost:4566 sqs send-message --queue-url http://sqs.eu-west-3.localhost.localstack.cloud:4566/000000000000/process-queue --message-body '{"url": "https://github.com/test/repo", "access_token": "test-token", "user_id": "test-user", "process_id": "test-process"}'

# 📭 Receive messages from SQS queue
receive-messages:
    @echo "Receiving messages from SQS queue..."
    aws --endpoint-url=http://localhost:4566 sqs receive-message --queue-url http://sqs.eu-west-3.localhost.localstack.cloud:4566/000000000000/process-queue --max-number-of-messages 10

# 📭 Clear messages from SQS queue
clear-messages:
    @echo "Clearing messages from SQS queue..."
    aws --endpoint-url=http://localhost:4566 sqs purge-queue --queue-url http://sqs.eu-west-3.localhost.localstack.cloud:4566/000000000000/process-queue

# 🚀 Start development environment
dev: start-services db-upgrade api-start

# 🚀 Start the Worker
worker-start:
    PROCESS_QUEUE_URL="http://sqs.eu-west-3.localhost.localstack.cloud:4566/000000000000/process-queue" \
    uv run src/issue_solver/worker/local_runner.py

# 🔍 Check LocalStack status
check-localstack:
    @echo "Checking LocalStack status..."
    docker ps | grep issue-solver-localstack || echo "LocalStack container is not running"
    curl -s http://localhost:4566/_localstack/health || echo "LocalStack is not accessible"

# ⬆️ Upgrade Database migrations with Alembic 🐍🐘⬆️
db-upgrade:
    uv run alembic upgrade head

# 🔑 Generate encryption key for token security
generate-encryption-key:
    @uv run python -c "from cryptography.fernet import Fernet; print(f'TOKEN_ENCRYPTION_KEY={Fernet.generate_key().decode()}')"

# 🗂️ Plan vector store cleanup (shows what would be deleted)
plan-cleanup:
    @echo "Planning vector store cleanup..."
    @uv run python scripts/vector_store_cleanup.py plan

# 🧹 Execute vector store cleanup (with confirmation)
cleanup:
    @echo "Executing vector store cleanup..."
    @uv run python scripts/vector_store_cleanup.py cleanup

# 🧪 Dry run vector store cleanup (shows actions without executing)
cleanup-dry-run:
    @echo "Running vector store cleanup in dry-run mode..."
    @uv run python scripts/vector_store_cleanup.py cleanup --dry-run

# 🏗️ Create development environment snapshot
create-dev-snapshot id="" deps="" commands="":
    @echo "Creating development environment snapshot..."
    uv run python scripts/create_snapshot.py --deps="{{deps}}" --commands="{{commands}}" --knowledge-base-id="{{id}}"

