name: SanStudent Multi-Project Deployment
run-name: ${{ github.actor }} wdraża system SanStudent 🚀

on:
  push:
    branches:
      - main
    paths:
      - 'Api/**'
      - 'FrontAdmin/**'
      - 'FrontStudent/**'
      - 'Common/**'
      - 'sanstudent.sln'
      - '.gitea/workflows/**'

jobs:
  # ==========================================
  # 1. API
  # ==========================================
  deploy-api:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout kodu
        uses: actions/checkout@v3

      - name: Build API Image
        run: docker build -t sanstudent-api:latest -f Api/Dockerfile .

      - name: Deploy API Container
        run: |
          docker rm -f sanstudent-api || true
          docker network create san-network || true
          docker run -d \
            --name sanstudent-api \
            --network san-network \
            -p 8083:8080 \
            -e ConnectionStrings__DefaultConnection="${{ secrets.DB_CONNECTION_STRING }}" \
            -e ConnectionStrings__RedisCache="${{ secrets.REDIS_CONNECTION_STRING }}" \
            --restart always \
            sanstudent-api:latest

  # ==========================================
  # 2. FRONTADMIN (Blazor WebAssembly)
  # ==========================================
  deploy-frontadmin:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout kodu
        uses: actions/checkout@v3

      - name: Build FrontAdmin Image
        run: docker build -t sanstudent-admin:latest -f FrontAdmin/Dockerfile .

      - name: Deploy FrontAdmin Container
        run: |
          docker rm -f sanstudent-admin || true
          docker run -d \
            --name sanstudent-admin \
            -p 8081:80 \
            --restart always \
            sanstudent-admin:latest

  # ==========================================
  # 3. FRONTSTUDENT (Blazor WebAssembly)
  # ==========================================
  deploy-frontstudent:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout kodu
        uses: actions/checkout@v3

      - name: Build FrontStudent Image
        run: docker build -t sanstudent-student:latest -f FrontStudent/Dockerfile .

      - name: Deploy FrontStudent Container
        run: |
          docker rm -f sanstudent-student || true
          docker run -d \
            --name sanstudent-student \
            -p 8082:80 \
            --restart always \
            sanstudent-student:latest