name: CI/CD Pipeline

on:
  push:
    branches:
      - develop
      - main
  pull_request:
    branches:
      - develop
      - main

jobs:
  flutter-build:
    name: Flutter Build & Test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.24.0'
          channel: 'stable'

      - name: Get dependencies (user_app)
        working-directory: frontend/user_app
        run: flutter pub get

      - name: Run analysis (user_app)
        working-directory: frontend/user_app
        run: flutter analyze --fatal-infos

      - name: Run tests (user_app)
        working-directory: frontend/user_app
        run: flutter test

      - name: Build web (user_app)
        working-directory: frontend/user_app
        run: flutter build web --release --base-href /user/

      - name: Get dependencies (rider_app)
        working-directory: frontend/rider_app
        run: flutter pub get

      - name: Run analysis (rider_app)
        working-directory: frontend/rider_app
        run: flutter analyze --fatal-infos

      - name: Run tests (rider_app)
        working-directory: frontend/rider_app
        run: flutter test

      - name: Build web (rider_app)
        working-directory: frontend/rider_app
        run: flutter build web --release --base-href /rider/

  web-build:
    name: Web Frontend Build & Test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - name: Install dependencies (platform_web)
        working-directory: frontend/platform_web
        run: npm ci

      - name: Run ESLint (platform_web)
        working-directory: frontend/platform_web
        run: npm run lint

      - name: Run TypeScript check (platform_web)
        working-directory: frontend/platform_web
        run: npm run typecheck

      - name: Build (platform_web)
        working-directory: frontend/platform_web
        run: npm run build

      - name: Install dependencies (merchant_web)
        working-directory: frontend/merchant_web
        run: npm ci

      - name: Run ESLint (merchant_web)
        working-directory: frontend/merchant_web
        run: npm run lint

      - name: Run TypeScript check (merchant_web)
        working-directory: frontend/merchant_web
        run: npm run typecheck

      - name: Build (merchant_web)
        working-directory: frontend/merchant_web
        run: npm run build

      - name: Install dependencies (official_website)
        working-directory: frontend/official_website
        run: npm ci

      - name: Build (official_website)
        working-directory: frontend/official_website
        run: npm run build

  backend-build:
    name: Backend Build & Test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up JDK
        uses: actions/setup-java@v4
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: maven

      - name: Build backend
        working-directory: services
        run: mvn clean install -DskipTests

      - name: Run tests
        working-directory: services
        run: mvn test

  security-check:
    name: Security Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Run Trivy vulnerability scanner
        uses: aquasecurity/trivy-action@master
        with:
          scan-type: 'fs'
          scan-ref: '.'
          severity: 'CRITICAL,HIGH'
          exit-code: '1'
          ignore-unfixed: true

      - name: Check for secrets
        uses: trufflesecurity/trufflehog@main
        with:
          path: ./
          base: ${{ github.event.repository.default_branch }}
          extra_args: --only-verified

  deploy:
    needs: [ flutter-build, web-build, backend-build, security-check ]
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Deploy to server
        run: |
          echo "Deploying to production server..."
          # Add your deployment script here
