name: Pinepods CI

on:
  pull_request:
    branches: [main]
  push:
    branches: [main]
  workflow_dispatch:

jobs:
  backend-tests:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:latest
        env:
          POSTGRES_USER: test_user
          POSTGRES_PASSWORD: test_password
          POSTGRES_DB: test_db
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.x"

      - name: Setup test environment
        run: |
          chmod +x ./setup-tests.sh
          ./setup-tests.sh

      - name: Run backend tests
        env:
          TEST_MODE: true
          DB_HOST: localhost
          DB_PORT: 5432
          DB_USER: test_user
          DB_PASSWORD: test_password
          DB_NAME: test_db
          DB_TYPE: postgresql
          TEST_DB_TYPE: postgresql
          PYTHONPATH: ${{ github.workspace }}
        run: |
          chmod +x ./run-tests.sh
          ./run-tests.sh postgresql

  frontend-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: hecrj/setup-rust-action@v2
        with:
          rust-version: 1.89
          targets: wasm32-unknown-unknown

      # Install cargo-binstall for other OSes using the standard method
      - name: Install cargo-binstall
        if: matrix.os != 'macos-latest'
        uses: cargo-bins/cargo-binstall@main

      - name: Depends install
        if: ${{ env.DEPENDS_SETUP == 'true' }}
        run: |
          sudo apt update
          sudo apt install -qy libgtk-3-dev
          sudo apt-get install -y libwebkit2gtk-4.0-dev libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf

      - name: wasm-addition
        run: |
          rustup target add wasm32-unknown-unknown

      - name: Install Trunk
        run: |
          cargo binstall trunk -y

      - name: Run frontend tests
        working-directory: ./web
        run: |
          RUSTFLAGS="--cfg=web_sys_unstable_apis" cargo test --features server_build -- --nocapture

  # docker-build:
  #   runs-on: ubuntu-latest
  #   needs: [backend-tests, frontend-tests]
  #   steps:
  #     - uses: actions/checkout@v3

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

  #     - name: Build and test Docker image
  #       run: |
  #         docker build -t pinepods:test .
  #         docker run --rm pinepods:test /bin/sh -c "python3 -m pytest /pinepods/tests/"
