# GitLab CI/CD Configuration for FastAPI
stages:
  - test
  - deploy
  - release

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.pip_cache"
  VIRTUAL_ENV: ".venv"

# Cache for dependencies
cache:
  paths:
    - .pip_cache/

before_script: # common setup for every job
  - python3.12 -m venv $VIRTUAL_ENV
  - source $VIRTUAL_ENV/bin/activate
  - pip install --upgrade pip

test:
  stage: test
  script:
    - source $VIRTUAL_ENV/bin/activate
    - pip install -r requirements.txt
    - export PYTHONPATH=$PYTHONPATH:/home/gitlab-runner/builds/t3_38ooWt/0/mx/aaredb/backend
    - cd /home/gitlab-runner/builds/t3_38ooWt/0/mx/aaredb  # Change to the project root
#pytest --cov=app --cov-report=xml  # Run tests and generate coverage report

lint:
  stage: test
  script:
    - source $VIRTUAL_ENV/bin/activate
    - pip install pre-commit black flake8
    - >
      pre-commit run --all-files || { 
        echo "Pre-commit formatting failed. Auto-fixing formatting issues with black..." && 
        black backend/ && 
        exit 0; 
      }

deploy:
  stage: deploy
  variables:
    CI: "true"        # Mark this as a CI run
    ENVIRONMENT: test  # Use test environment settings
    PORT: 8081
  script:
    - echo "Setting up Python dependencies..."
    - source $VIRTUAL_ENV/bin/activate
    - pip install -r requirements.txt
    - echo "Starting the application to test startup with SSL..."
    - cd $CI_PROJECT_DIR/backend  # Use GitLab-provided dynamic root path
    - python -m main              # Run the main module dynamically

release:
  stage: release
  when: manual
  variables:
    TWINE_USERNAME: gitlab-ci-token
    TWINE_PASSWORD: $CI_JOB_TOKEN
    ENVIRONMENT: test
  before_script:
    - cp ~/.env.test $CI_PROJECT_DIR/frontend/.
    - python3.12 -m venv $VIRTUAL_ENV
    - source $VIRTUAL_ENV/bin/activate
    - pip install --upgrade pip
    # Explicit clean-up commands
    - find "" -name '__pycache__' -type d -exec rm -rf {} + || true
    - find "$CI_PROJECT_DIR" -name '*.pyc' -type f -delete || true
    # Fix permissions (if necessary)
    - chmod -R u+w "$CI_PROJECT_DIR"

  script:
    - ls -la "$CI_PROJECT_DIR/config_${ENVIRONMENT}.json"  # <-- Verify host file
    - file "$CI_PROJECT_DIR/config_${ENVIRONMENT}.json"    # <-- Confirm host file type
    # build and run commands within docker container context
    - docker compose --env-file frontend/.env.${ENVIRONMENT} build backend


    # Run commands inside your 'backend' service container
    - docker compose --env-file frontend/.env.${ENVIRONMENT} run --rm backend mkdir -p /app/backend/ssl
    - docker compose --env-file frontend/.env.${ENVIRONMENT} run --rm backend bash make_openapi_client.sh

    # After script finishes execution within the container,
    # revert to the runner environment context if needed
    # Assuming 'python-client' is generated and mounted correctly,
    # subsequent twine commands must be run out of the container

    - ls backend/python-client/dist/  # Ensure files exist

    # install further publishing requirements outside of docker
    - pip install --upgrade build setuptools wheel twine
    - twine check backend/python-client/dist/*
    - twine upload --repository-url ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi backend/python-client/dist/*