image: python:3.13

stages:
#  - lint
  - test
#  - deploy

variables:
  UV_CACHE_DIR: "$CI_PROJECT_DIR/.uv-cache"

cache:
  paths:
    - .uv-cache/

.uv_setup: &uv_setup
  - curl -LsSf https://astral.sh/uv/install.sh | sh
  - export PATH="$HOME/.cargo/bin:$PATH"
  - source $HOME/.local/bin/env

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
    - if: $CI_COMMIT_TAG

#lint:
#  stage: lint
#  before_script:
#    - *uv_setup
#  script:
#    - uv sync
#    - uv run ruff check .

# --- Data platform ---

test:cli:
  stage: test
  before_script:
    - *uv_setup
  script:
    - uv sync
    - uv run pytest tests
  rules:
    - changes:
        - src/**/*
        - tests/**/*
        - pyproject.toml

test:sqlmesh:
  stage: test
  before_script:
    - *uv_setup
  script:
    - uv sync
    - cd transform/sqlmesh_materia && uv run sqlmesh test
  rules:
    - changes:
        - transform/**/*

# --- Web app ---

test:web:
  stage: test
  before_script:
    - *uv_setup
  script:
    - uv sync
    - cd web && uv run pytest tests/ -x -q
    - cd web && uv run ruff check src/ tests/
  rules:
    - changes:
        - web/**/*

#deploy:web:
#  stage: deploy
#  image: alpine:latest
#  needs: [test:web]
#  rules:
#    - if: $CI_COMMIT_BRANCH == "master"
#      changes:
#        - web/**/*
#  before_script:
#    - apk add --no-cache openssh-client
#    - eval $(ssh-agent -s)
#    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
#    - mkdir -p ~/.ssh
#    - chmod 700 ~/.ssh
#    - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
#  script:
#    - |
#      ssh "$DEPLOY_USER@$DEPLOY_HOST" "cat > /opt/beanflows/beanflows/.env" << ENVEOF
#      APP_NAME=$APP_NAME
#      SECRET_KEY=$SECRET_KEY
#      BASE_URL=$BASE_URL
#      DEBUG=false
#      ADMIN_PASSWORD=$ADMIN_PASSWORD
#      DATABASE_PATH=data/app.db
#      MAGIC_LINK_EXPIRY_MINUTES=${MAGIC_LINK_EXPIRY_MINUTES:-15}
#      SESSION_LIFETIME_DAYS=${SESSION_LIFETIME_DAYS:-30}
#      RESEND_API_KEY=$RESEND_API_KEY
#      EMAIL_FROM=${EMAIL_FROM:-hello@example.com}
#      ADMIN_EMAIL=${ADMIN_EMAIL:-}
#      RATE_LIMIT_REQUESTS=${RATE_LIMIT_REQUESTS:-100}
#      RATE_LIMIT_WINDOW=${RATE_LIMIT_WINDOW:-60}
#      PADDLE_API_KEY=$PADDLE_API_KEY
#      PADDLE_WEBHOOK_SECRET=$PADDLE_WEBHOOK_SECRET
#      PADDLE_PRICE_STARTER=$PADDLE_PRICE_STARTER
#      PADDLE_PRICE_PRO=$PADDLE_PRICE_PRO
#      ENVEOF
#    - ssh "$DEPLOY_USER@$DEPLOY_HOST" "chmod 600 /opt/beanflows/beanflows/.env"
#    - ssh "$DEPLOY_USER@$DEPLOY_HOST" "cd /opt/beanflows && git pull origin master && ./deploy.sh"
