# CI for shopdb-flask.
#
# The Gitea runner executes jobs on the host (act host executor), so
# actions/setup-python cannot provision interpreters (no prebuilt binaries
# for the host distro). The backend job uses the system python3 in a venv
# instead. setup-node works because node is resolved differently.
#
# Jobs run on push and pull_request:
#   backend        - pytest (tests use in-memory SQLite via TestingConfig, so no
#                    database service is needed).
#   naming         - the CONTRIBUTING.md naming/style gate.
#   frontend       - Vue build.
#   migrations-mysql - proves the REAL multi-site deploy path: a fresh
#                    `flask db upgrade` + per-plugin install on utf8mb4 MySQL
#                    from empty, idempotent on a second run. The pytest suite
#                    only exercises SQLite create_all(), so without this a
#                    regression in the Alembic chain on MySQL would ship
#                    undetected. Needs a runner that supports service
#                    containers; if yours does not, run these steps against a
#                    host MySQL instead.

name: CI

on:
  push:
  pull_request:

jobs:
  backend:
    runs-on: ubuntu-latest
    steps:
      - name: Check out
        uses: actions/checkout@v4
      - name: Install dependencies
        run: |
          python3 --version
          python3 -m venv .venv
          .venv/bin/pip install --upgrade pip
          .venv/bin/pip install -r requirements.txt
      - name: Run backend tests
        run: .venv/bin/python -m pytest

  naming:
    runs-on: ubuntu-latest
    steps:
      - name: Check out
        uses: actions/checkout@v4
      - name: Run naming and style check
        run: bash scripts/check-naming-and-style.sh

  frontend:
    runs-on: ubuntu-latest
    steps:
      - name: Check out
        uses: actions/checkout@v4
      - name: Set up Node
        uses: actions/setup-node@v4
        with:
          node-version: '20'
      - name: Install and build frontend
        run: |
          npm ci
          npm run build
        working-directory: frontend

  migrations-mysql:
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: shopdb_ci
        ports:
          - 3306:3306
        options: >-
          --health-cmd="mysqladmin ping -h localhost -uroot -proot"
          --health-interval=5s --health-timeout=5s --health-retries=20
    env:
      DATABASE_URL: mysql+pymysql://root:root@127.0.0.1:3306/shopdb_ci?charset=utf8mb4
      SECRET_KEY: ci-secret
      JWT_SECRET_KEY: ci-jwt-secret
    steps:
      - name: Check out
        uses: actions/checkout@v4
      - name: Install dependencies
        run: |
          python3 -m venv .venv
          .venv/bin/pip install --upgrade pip
          .venv/bin/pip install -r requirements.txt
      - name: Force utf8mb4 on the CI database
        run: |
          mysql -h 127.0.0.1 -uroot -proot -e \
            "ALTER DATABASE shopdb_ci CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
      - name: Fresh core upgrade from empty
        run: .venv/bin/flask db upgrade
      - name: Install every bundled plugin (runs its chain)
        run: |
          for p in computers employees geenforce knowledgebase machines \
                   measuringtools network notifications printers slides usb warranty; do
            .venv/bin/flask plugin install "$p"
          done
      - name: Assert schema built + utf8mb4, and a second upgrade is a no-op
        run: |
          .venv/bin/python - <<'PY'
          from shopdb import create_app
          from shopdb.extensions import db
          from sqlalchemy import text
          app = create_app()
          with app.app_context():
              insp = db.inspect(db.engine)
              tables = insp.get_table_names()
              assert len(tables) >= 70, f'only {len(tables)} tables built'
              row = db.session.execute(text(
                  "SELECT default_character_set_name FROM information_schema.schemata "
                  "WHERE schema_name = 'shopdb_ci'")).first()
              assert row[0] == 'utf8mb4', f'charset is {row[0]}, not utf8mb4'
              print(f'OK: {len(tables)} tables, charset {row[0]}')
          PY
      - name: Second core upgrade must be a clean no-op
        run: .venv/bin/flask db upgrade
