image: python:3.10-bullseye

stages:
  - build
  - deploy

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
  GIT_SUBMODULE_STRATEGY: recursive

cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  - python -V
  - pip install virtualenv
  - virtualenv venv
  - source venv/bin/activate
  - pip install -r requirements.txt
  - pip install teachbooks

build-book:
  stage: build
  script:
    # Previous build steps...
    - |
      if [ "$CI_COMMIT_BRANCH" == "release" ]; then
        echo "Building production version"
        teachbooks build --publish book/ > >(tee stdout.log) 2> >(tee stderr.log >&2)
      else
        echo "Building draft version"
        teachbooks build book/ > >(tee stdout.log) 2> >(tee stderr.log >&2)
      fi

    # Size analysis
# Debug directory sizes
    - echo "=== Size Analysis ===" | tee size_analysis.log
    - cd book/_build
    
    # Overall sizes
    - echo "Overall directory sizes:" >> ../../size_analysis.log
    - du -sh * >> ../../size_analysis.log
    
    # HTML directory breakdown
    - echo -e "\nHTML directory breakdown:" >> ../../size_analysis.log
    - cd html
    - echo "JavaScript files:" >> ../../../size_analysis.log
    - du -ch $(find . -name "*.js") | tail -n1 >> ../../../size_analysis.log
    
    - echo "CSS files:" >> ../../../size_analysis.log
    - du -ch $(find . -name "*.css") | tail -n1 >> ../../../size_analysis.log
    
    - echo "HTML files:" >> ../../../size_analysis.log
    - du -ch $(find . -name "*.html") | tail -n1 >> ../../../size_analysis.log
    
    # Find largest files
    - echo -e "\nLargest files (>1MB):" >> ../../../size_analysis.log
    - find . -type f -size +1M -exec ls -lh {} \; | sort -hr >> ../../../size_analysis.log
    
    - cd ../../..

    - echo $CI_JOB_ID > build_job_id.txt

  artifacts:
    paths:
      # Essential web content
      - book/_build/html/**/*.{html,css,js}
      - book/_build/html/_images/**
      - book/_build/html/_static/**
      - book/_build/html/_custom_downloads/**
      - book/_build/html/genindex.html
      - book/_build/html/search.html
      - book/_build/html/**/*.py
      
      # Required for interactive features
      - book/_build/jupyter_execute/**/*.html
      - book/_build/html/_static/scripts/**
      
      # Logs and build info
      - build_job_id.txt
      - build_debug.log
      - stdout.log
      - stderr.log

    exclude:
      # Development/build files
      - "**/*.js.map"
      - "**/_sources/**"
      - "**/__pycache__/**"
      - "**/*.pyc"
      - "**/*.pickle"
      - book/_build/.doctrees/**
      - book/_build/jupyter_cache/**
      # Keep notebooks for interactive features
      # - "**/*.ipynb"
      
      # # Build artifacts
      # - "**/__pycache__/**"
      # - "**/*.pyc"
      # - book/_build/.doctrees/**
      # - book/_build/jupyter_cache/**
      # - "**/*.pickle"

    expire_in: 1 week

deploy-draft:
  stage: deploy
  needs: ["build-book"]
  script:
    - BUILD_JOB_ID=$(cat build_job_id.txt)
    - |
      curl -X POST https://mude.citg.tudelft.nl/hooks/book-deploy-draft \
      -H "Content-Type: application/json" \
      -H "X-Gitlab-Token: glpat-m4CzsDqHnXNn3Pf5Whyd" \
      -d "{\"object_kind\":\"pipeline\",\"object_attributes\":{\"status\":\"success\",\"ref\":\"main\"},\"build_job_id\":$BUILD_JOB_ID}"
  environment:
    name: draft
    url: https://mude.citg.tudelft.nl/2024/book/draft
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

deploy-production:
  stage: deploy
  needs: ["build-book"]
  script:
    - BUILD_JOB_ID=$(cat build_job_id.txt)
    - |
      curl -X POST https://mude.citg.tudelft.nl/hooks/book-deploy-production \
      -H "Content-Type: application/json" \
      -H "X-Gitlab-Token: glpat-m4CzsDqHnXNn3Pf5Whyd" \
      -d "{\"object_kind\":\"pipeline\",\"object_attributes\":{\"status\":\"success\",\"ref\":\"release\"},\"build_job_id\":$BUILD_JOB_ID}"
  environment:
    name: production
    url: https://mude.citg.tudelft.nl/2024/book
  rules:
    - if: $CI_COMMIT_BRANCH == "release"