name: Actions
on:
  push:
    branches:
      - main
  pull_request:

jobs:
  lint:
    name: Lint Code (Ruff & Pylint)
    runs-on: docker
    container: catthehacker/ubuntu:act-latest@sha256:70d7485966a50a639ddab37445fd27c2f0b5086ad4959ec3bba228ed394c1928
    steps:
      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4

      - name: "Setup uv"
        uses: actions/setup-uv@22695119d769bdb6f7032ad67b9bca0ef8c4a174 # v5
        with:
          version: "latest"
          enable-cache: true
          prune-cache: false
          github-token: ${{ secrets.GITHUBTOKEN }}

      - name: "Install dependencies"
        run: uv sync

      - name: Analysing code with Ruff
        run: uv run ruff check $(git ls-files '*.py')
        continue-on-error: true

      - name: Analysing code with Pylint
        run: uv run pylint --rcfile=.forgejo/workflows/config/.pylintrc $(git ls-files '*.py')

  docs:
    name: Build Documentation (MkDocs)
    runs-on: docker
    container: catthehacker/ubuntu:act-latest@sha256:70d7485966a50a639ddab37445fd27c2f0b5086ad4959ec3bba228ed394c1928
    steps:
      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
        with:
          fetch-depth: 0

      - name: "Setup uv"
        uses: actions/setup-uv@22695119d769bdb6f7032ad67b9bca0ef8c4a174 # v5
        with:
          version: "latest"
          enable-cache: true
          prune-cache: false
          github-token: ${{ secrets.GITHUBTOKEN }}

      - name: Install dependencies
        run: uv sync --no-dev --group=documentation

      - name: Set environment variables
        uses: actions/env@1791216cd180e6578dd1d67fb8d2852b883a5f53 # v2

      - name: Build documentation
        run: |
          export SITE_URL="https://$CI_ACTION_REF_NAME_SLUG.seacogs.coastalcommits.com"
          export EDIT_URI="src/branch/$CI_ACTION_REF_NAME/.docs"
          uv run mkdocs build -v

      - name: Deploy documentation
        run: |
          GREEN='\033[0;32m'
          YELLOW='\033[0;33m'
          BLUE='\033[0;34m'

          unset GITHUB_TOKEN
          unset GITLAB_TOKEN

          echo "${YELLOW}Deploying to ${BLUE}Meli ${YELLOW}on branch ${GREEN}$CI_ACTION_REF_NAME_SLUG${YELLOW}...\n"

          npx -p "@getmeli/cli" meli upload ./site \
            --url "https://meli.csw.im" \
            --site "${{ vars.MELI_SITE_ID }}" \
            --token "${{ secrets.MELI_TOKEN }}" \
            --release "$CI_ACTION_REF_NAME_SLUG/${{ env.GITHUB_SHA }}" \
            --branch "$CI_ACTION_REF_NAME_SLUG"

          echo "\n${YELLOW}Deployed to ${BLUE}Meli ${YELLOW}on branch ${GREEN}$CI_ACTION_REF_NAME_SLUG${YELLOW}!"
          echo "${GREEN}https://$CI_ACTION_REF_NAME_SLUG.seacogs.coastalcommits.com/"
        env:
          GITEA_TOKEN: ${{ secrets.COASTALCOMMITSTOKEN }}