#!/bin/bash

set -e

VENV=""

for dir in .venv venv env; do
    if [ -f "$dir/bin/activate" ]; then
        VENV="$dir"
        break
    fi
done

if [ -z "$VENV" ]; then
    echo "ERROR: No virtual environment found."
    echo "Expected .venv/, venv/ or env/"
    exit 1
fi

echo "Using virtual environment: $VENV"

source "$VENV/bin/activate"

if [ ! -f requirements.txt ]; then
    echo "ERROR: requirements.txt not found."
    exit 1
fi

echo "Checking requirements..."
python -m pip install -r requirements.txt

case "$1" in
    local)
        echo "Starting Calendar API locally..."
        python -m uvicorn main:app --reload
        ;;

    lan)
        echo "Starting Calendar API on LAN..."
        python -m uvicorn main:app --host 0.0.0.0 --port 8000 --reload
        ;;

    *)
        echo "Usage: $0 {local|lan}"
        echo "  local  Start locally"
        echo "  lan    Start on LAN"
        exit 1
        ;;
esac