# This Evennia workflow will install Python dependencies, run tests with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: test-suite

on:
  push:
    branches: [main, develop]
    paths-ignore:
      - "docs/**"
  pull_request:
    branches: [main, develop]

jobs:
  test-sqlite:
    name: Test (SQLite)
    runs-on: ubuntu-latest

    strategy:
      fail-fast: true
      matrix:
        python-version: ["3.12", "3.13", "3.14"]
        include:
          - python-version: "3.12"
            coverage-test: true

    timeout-minutes: 30

    steps:
      - uses: actions/checkout@v4

      - name: Set up database (sqlite3)
        uses: ./.github/actions/setup-database
        with:
          database: sqlite3
        timeout-minutes: 5

      - name: Run tests
        uses: ./.github/actions/run-tests
        with:
          python-version: ${{ matrix.python-version }}
          testing-db: sqlite3
          coverage-test: ${{ matrix.coverage-test == true && 'true' || 'false' }}
          needs-postgres-package: "false"
          needs-mysql-package: "false"
          coveralls-token: ${{ secrets.COVERALLS_REPO_TOKEN }}

  test-mysql:
    name: Test (MySQL)
    runs-on: ubuntu-latest

    strategy:
      fail-fast: true
      matrix:
        python-version: ["3.12", "3.13", "3.14"]

    timeout-minutes: 35

    steps:
      - uses: actions/checkout@v4

      - name: Set up MySQL
        uses: shogo82148/actions-setup-mysql@v1
        with:
          mysql-version: "8.0"
          root-password: root_password
          user: evennia
          password: password
          my-cnf: |
            [mysqld]
            character-set-server=utf8mb3
            collation-server=utf8mb3_unicode_ci
            innodb_default_row_format=DYNAMIC
            innodb_file_per_table=1

      - name: Set up database (mysql)
        uses: ./.github/actions/setup-database
        with:
          database: mysql
        timeout-minutes: 5

      - name: Run tests
        uses: ./.github/actions/run-tests
        with:
          python-version: ${{ matrix.python-version }}
          testing-db: mysql
          coverage-test: "false"
          needs-postgres-package: "false"
          needs-mysql-package: "true"

  test-postgresql:
    name: Test (PostgreSQL)
    runs-on: ubuntu-latest

    strategy:
      fail-fast: true
      matrix:
        python-version: ["3.12", "3.13", "3.14"]

    timeout-minutes: 60

    services:
      postgres:
        image: postgres:14
        env:
          POSTGRES_DB: evennia
          POSTGRES_USER: evennia
          POSTGRES_PASSWORD: password
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval=10s
          --health-timeout=5s
          --health-retries=5

    steps:
      - uses: actions/checkout@v4

      - name: Install PostgreSQL client
        run: |
          sudo apt-get update
          sudo apt-get install -y postgresql-client
        timeout-minutes: 2

      - name: Set up database (postgresql)
        uses: ./.github/actions/setup-database
        with:
          database: postgresql
        timeout-minutes: 5

      - name: Run tests
        uses: ./.github/actions/run-tests
        with:
          python-version: ${{ matrix.python-version }}
          testing-db: postgresql
          coverage-test: "false"
          needs-postgres-package: "true"
          needs-mysql-package: "false"

  deploy:
    name: Deploy Docker Image
    needs: [test-sqlite, test-mysql, test-postgresql]
    runs-on: ubuntu-latest
    if: ${{ github.repository == 'evennia/evennia' && github.ref == 'refs/heads/main' }}
    steps:
      - uses: actions/checkout@v3

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and push for main
        if: ${{ github.ref == 'refs/heads/main' }}
        id: docker_build_main
        uses: docker/build-push-action@v3
        with:
          push: true
          tags: evennia/evennia:latest

      - name: Build and push for develop
        if: ${{ github.ref == 'refs/heads/develop' }}
        id: docker_build_develop
        uses: docker/build-push-action@v3
        with:
          push: true
          tags: evennia/evennia:develop
