stages:
  - Test Stage
  - Build Stage

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

"Python Tests":
  stage: Test Stage
  image: python:3.14-alpine
  variables:
    PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
  cache:
    key:
      files:
        - backend/requirements.txt
    paths:
      - .cache/pip
  script:
    - echo "Running tests on the Python Backend..."
    - cd backend
    - pip install -r requirements.txt
    - pytest

"React Tests":
  stage: Test Stage
  image: node:alpine
  variables:
    npm_config_cache: "$CI_PROJECT_DIR/.npm-cache"
  cache:
    key:
      files:
        - frontend/package-lock.json
    paths:
      - .npm-cache
      - frontend/node_modules
  script:
    - cd frontend
    - npm ci --prefer-offline
    - npm test -- run --exclude "**/*.live.test.ts"

"Build Backend Image":
  stage: Build Stage
  image: docker:24.0.5
  rules:
    - if: $CI_COMMIT_TAG
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - cd backend
    - >
      docker build 
      --label "org.opencontainers.image.url=$CI_PROJECT_URL" 
      --label "org.opencontainers.image.source=$CI_PROJECT_URL"
      -t $CI_REGISTRY_IMAGE/backend:$CI_COMMIT_TAG 
      -t $CI_REGISTRY_IMAGE/backend:latest .
    - docker push $CI_REGISTRY_IMAGE/backend:$CI_COMMIT_TAG
    - docker push $CI_REGISTRY_IMAGE/backend:latest

"Build Frontend Image":
  stage: Build Stage
  image: docker:24.0.5
  rules:
    - if: $CI_COMMIT_TAG
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - cd frontend
    - >
      docker build 
      --label "org.opencontainers.image.url=$CI_PROJECT_URL" 
      --label "org.opencontainers.image.source=$CI_PROJECT_URL"
      -t $CI_REGISTRY_IMAGE/frontend:$CI_COMMIT_TAG 
      -t $CI_REGISTRY_IMAGE/frontend:latest .
    - docker push $CI_REGISTRY_IMAGE/frontend:$CI_COMMIT_TAG
    - docker push $CI_REGISTRY_IMAGE/frontend:latest
