# GitLab CI Configuration for satware.ai MkDocs Material Site
# Migrated from GitHub Actions with preview deployment support

stages:
  - build
  - deploy
  - cleanup

variables:
  PYTHON_VERSION: "3.13"
  MKDOCS_MATERIAL_VERSION: "9.6.14"
  PRODUCTION_URL: "https://satware.ai"
  PREVIEW_BASE_URL: "https://satware.ai"

# Build Job Template
.build_template:
  image: python:${PYTHON_VERSION}-slim
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - .cache/
      - venv/
  before_script:
    - apt-get update && apt-get install -y build-essential git libcairo2-dev libfreetype6-dev libffi-dev libjpeg-dev libpng-dev libz-dev
    - python -m venv venv
    - source venv/bin/activate
    - pip install --upgrade pip
    - pip install cairosvg libsass mkdocs-exclude mkdocs-git-revision-date-localized-plugin mkdocs-glightbox mkdocs-include-markdown-plugin mkdocs-literate-nav mkdocs-macros-plugin mkdocs-material==${MKDOCS_MATERIAL_VERSION} "mkdocs-material[imaging]" mkdocs-minify-plugin mkdocs-redirects mkdocs-rss-plugin mkdocs-snippets mkdocs-video watchdog

# Production Deployment
pages:
  extends: .build_template
  stage: deploy
  rules:
    - if: $CI_COMMIT_REF_NAME == "main-mkdocs"
    - if: $CI_COMMIT_REF_NAME == "main"
  script:
    - source venv/bin/activate
    - mkdir -p docs/assets/css
    - pysassc overrides/assets/css/custom.scss docs/assets/css/custom.css
    - echo "📍 Production deployment to ${PRODUCTION_URL}"
    - grep '^site_url:' mkdocs.yml || echo "No site_url found"
    - echo "🏗️ Building MkDocs site..."
    - mkdocs build --verbose
    - echo "📦 Preparing production deployment..."
    - mv site public
    - echo "DEPLOYMENT_URL=${PRODUCTION_URL}" > deploy.env
    - echo "DEPLOYMENT_TYPE=production" >> deploy.env
    - echo "BRANCH=${CI_COMMIT_REF_NAME}" >> deploy.env
    - echo "COMMIT=${CI_COMMIT_SHA}" >> deploy.env
    - echo "🚀 Production deployment ready"
    - echo "   📍 URL ${PRODUCTION_URL}"
    - echo "   🌿 Branch ${CI_COMMIT_REF_NAME}"
    - find public -type f | wc -l | xargs echo "   📦 Files"
    - du -sh public | cut -f1 | xargs echo "   💾 Size"
  artifacts:
    paths:
      - public
    reports:
      dotenv: deploy.env
  environment:
    name: production
    url: ${PRODUCTION_URL}

# Preview Deployment
preview:
  extends: .build_template
  stage: deploy
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_REF_NAME =~ /^(feature|fix|blog|test|preview)\/.*/
    - if: $CI_COMMIT_REF_NAME =~ /^(develop|staging)$/
  script:
    - source venv/bin/activate
    - mkdir -p docs/assets/css
    - pysassc overrides/assets/css/custom.scss docs/assets/css/custom.css
    - echo "🔍 Preview deployment for branch ${CI_COMMIT_REF_NAME}"
    - BRANCH_PATH=$(echo "${CI_COMMIT_REF_NAME}" | sed 's|feature/||g' | sed 's|fix/||g' | sed 's|blog/||g' | sed 's|[^a-zA-Z0-9-]|-|g' | tr '[:upper:]' '[:lower:]')
    - PREVIEW_URL="${PREVIEW_BASE_URL}/${BRANCH_PATH}/"
    - echo "📝 Updating site_url to ${PREVIEW_URL}"
    - cp mkdocs.yml mkdocs.yml.backup
    - sed -i "s|^site_url:.*|site_url: ${PREVIEW_URL}|" mkdocs.yml
    - sed -i "s|^use_directory_urls:.*|use_directory_urls: true|" mkdocs.yml
    - echo "✅ Updated mkdocs.yml for preview"
    - grep -E "^(site_url|use_directory_urls):" mkdocs.yml || echo "Config updated"
    - echo "🏗️ Building MkDocs site..."
    - mkdocs build --verbose
    - mkdir -p preview-deploy/${BRANCH_PATH}
    - cp -r site/* preview-deploy/${BRANCH_PATH}/
    - echo "DEPLOYMENT_URL=${PREVIEW_URL}" > deploy.env
    - echo "DEPLOYMENT_TYPE=preview" >> deploy.env
    - echo "BRANCH=${CI_COMMIT_REF_NAME}" >> deploy.env
    - echo "BRANCH_PATH=${BRANCH_PATH}" >> deploy.env
    - echo "COMMIT=${CI_COMMIT_SHA}" >> deploy.env
    - echo "# Preview Deployment Instructions" > DEPLOY_INSTRUCTIONS.md
    - echo "" >> DEPLOY_INSTRUCTIONS.md
    - echo "**Branch:** \`${CI_COMMIT_REF_NAME}\`" >> DEPLOY_INSTRUCTIONS.md
    - echo "**Preview URL:** ${PREVIEW_URL}" >> DEPLOY_INSTRUCTIONS.md
    - echo "**Commit:** \`${CI_COMMIT_SHA}\`" >> DEPLOY_INSTRUCTIONS.md
    - echo "" >> DEPLOY_INSTRUCTIONS.md
    - echo "## Manual Deployment Steps:" >> DEPLOY_INSTRUCTIONS.md
    - echo "" >> DEPLOY_INSTRUCTIONS.md
    - echo "1. Download the \`preview-deploy\` artifact from this pipeline" >> DEPLOY_INSTRUCTIONS.md
    - echo "2. Extract to your satware.ai server under path: \`/${BRANCH_PATH}/\`" >> DEPLOY_INSTRUCTIONS.md
    - echo "3. Ensure web server serves files from this location" >> DEPLOY_INSTRUCTIONS.md
    - echo "4. Access preview at: ${PREVIEW_URL}" >> DEPLOY_INSTRUCTIONS.md
    - echo "🔍 Preview deployment ready"
    - echo "   📍 URL ${PREVIEW_URL}"
    - echo "   🌿 Branch ${CI_COMMIT_REF_NAME}"
    - echo "   📁 Path /${BRANCH_PATH}/"
    - find preview-deploy -type f | wc -l | xargs echo "   📦 Files"
    - du -sh preview-deploy | cut -f1 | xargs echo "   💾 Size"
  artifacts:
    paths:
      - preview-deploy/
      - DEPLOY_INSTRUCTIONS.md
    expire_in: 7 days
    reports:
      dotenv: deploy.env
  environment:
    name: preview/${CI_COMMIT_REF_SLUG}
    url: ${PREVIEW_BASE_URL}/${BRANCH_PATH}/
    on_stop: cleanup_preview

# Cleanup Preview
cleanup_preview:
  stage: cleanup
  image: alpine:latest
  rules:
    - if: $CI_COMMIT_REF_NAME =~ /^(feature|fix|blog|test|preview)\/.*/
      when: manual
      allow_failure: true
  script:
    - BRANCH_PATH=$(echo "${CI_COMMIT_REF_NAME}" | sed 's|feature/||g' | sed 's|fix/||g' | sed 's|blog/||g' | sed 's|[^a-zA-Z0-9-]|-|g' | tr '[:upper:]' '[:lower:]')
    - echo "🧹 Cleanup preview environment for ${CI_COMMIT_REF_NAME}"
    - echo "Manual cleanup required - remove files from server path /${BRANCH_PATH}/"
  environment:
    name: preview/${CI_COMMIT_REF_SLUG}
    action: stop

# Build Information
build_info:
  stage: build
  image: alpine:latest
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_REF_NAME =~ /.*/
  script:
    - echo "🔍 Build Information"
    - echo "   📁 Repository ${CI_PROJECT_PATH}"
    - echo "   🌿 Branch ${CI_COMMIT_REF_NAME}"
    - echo "   📝 Commit ${CI_COMMIT_SHA:0:8}"
    - echo "   👤 Author ${GITLAB_USER_NAME}"
    - echo "   📅 Pipeline ${CI_PIPELINE_ID}"
    - echo "   🔗 URL ${CI_PIPELINE_URL}"
    - |
      if [ "$CI_COMMIT_REF_NAME" = "main" ] || [ "$CI_COMMIT_REF_NAME" = "main-mkdocs" ]; then
        echo "   🚀 Deployment: Production (${PRODUCTION_URL})"
      else
        BRANCH_PATH=$(echo "${CI_COMMIT_REF_NAME}" | sed 's|feature/||g' | sed 's|fix/||g' | sed 's|blog/||g' | sed 's|[^a-zA-Z0-9-]|-|g' | tr '[:upper:]' '[:lower:]')
        echo "   🔍 Deployment: Preview (${PREVIEW_BASE_URL}/${BRANCH_PATH}/)"
      fi