# When you push to the develop branch or open/update a pull request targeting main, Gitea will:
# Trigger the "CI Checks" workflow.
# Execute the checks job on a runner.
# Run each step sequentially.
# If any of the linter/formatter check commands (black --check, ruff check, npm run lint) exit with a non-zero status code (indicating an error or check failure), the step and the entire job will fail.
# You will see the status (success/failure) associated with your commit or pull request in the Gitea interface.

name: CI Checks

# Define triggers for the workflow
on:
  push:
    branches:
      - develop # Run on pushes to the develop branch
  pull_request:
    branches:
      - main # Run on pull requests targeting the main branch

jobs:
  checks:
    name: Linters and Formatters
    runs-on: ubuntu-latest # Use a standard Linux runner environment

    steps:
      - name: Checkout code
        uses: actions/checkout@v4 # Fetches the repository code

      # --- Backend Checks (Python/FastAPI) ---

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11' # Match your project/Dockerfile version
          cache: 'pip' # Cache pip dependencies based on requirements.txt
          cache-dependency-path: 'be/requirements.txt' # Specify path for caching

      - name: Install Backend Dependencies and Tools
        working-directory: ./be # Run command within the 'be' directory
        run: |
          pip install --upgrade pip
          pip install -r requirements.txt
          pip install black ruff # Install formatters/linters for CI check

      - name: Run Black Formatter Check (Backend)
        working-directory: ./be
        run: black --check --diff .

      - name: Run Ruff Linter (Backend)
        working-directory: ./be
        run: ruff check .

      # --- Frontend Checks (SvelteKit/Node.js) ---

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20.x' # Or specify your required Node.js version (e.g., 'lts/*')
          cache: 'npm' # Or 'pnpm' / 'yarn' depending on your package manager
          cache-dependency-path: 'fe/package-lock.json' # Adjust lockfile name if needed

      - name: Install Frontend Dependencies
        working-directory: ./fe # Run command within the 'fe' directory
        run: npm install # Or 'pnpm install' / 'yarn install'

      - name: Run ESLint and Prettier Check (Frontend)
        working-directory: ./fe
        # Assuming you have a 'lint' script in fe/package.json that runs both
        # Example package.json script: "lint": "prettier --check . && eslint ."
        run: npm run lint
        # If no combined script, run separately:
        # run: |
        #   npm run format -- --check # Or 'npx prettier --check .'
        #   npm run lint # Or 'npx eslint .'

      # - name: Run Frontend Type Check (Optional but recommended)
      #   working-directory: ./fe
      #   # Assuming you have a 'check' script: "check": "svelte-kit sync && svelte-check ..."
      #   run: npm run check

      # - name: Run Placeholder Tests (Optional)
      #   run: |
      #     # Add commands to run backend tests if available
      #     # Add commands to run frontend tests (e.g., npm test in ./fe) if available
      #     echo "No tests configured yet."