name: Snapshot Admin Web Validation

on:
  workflow_dispatch:
  push:
    paths:
      - "src/quant_engine/snapshot_admin_server_v1.py"
      - "src/quant_engine/snapshot_admin_store_v1.py"
      - "tools/run_snapshot_admin_server_v1.py"
      - "tools/validate_snapshot_admin_workflow_v1.py"
      - "tools/validate_snapshot_admin_web_v1.py"
      - "spec/15_account_snapshot_contract.yaml"
      - "spec/18_settings_contract.yaml"
      - "GatherTradingData.json"

jobs:
  # Push-only smoke gate: no deployment, no web UI smoke, no long-running side effects.
  validate-snapshot-admin-smoke:
    if: github.event_name == 'push'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        run: |
          echo "[smoke] push-only snapshot admin workflow validation"
          if [ -d .git ]; then
            git remote set-url origin http://x-access-token:${{ secrets.GITHUB_TOKEN }}@192.168.123.100:8418/KimJaeHyun/myfinance.git
          else
            git init
            git remote add origin http://x-access-token:${{ secrets.GITHUB_TOKEN }}@192.168.123.100:8418/KimJaeHyun/myfinance.git
          fi
          git fetch origin main --depth=1
          git reset --hard FETCH_HEAD

      - name: Setup Python Environment
        run: |
          echo "[smoke] prepare python venv"
          VENV_BASE=/volume1/gitea/python_venv
          REQ_HASH=$(md5sum tools/validate_snapshot_admin_workflow_v1.py 2>/dev/null | cut -d' ' -f1 || echo "snapshot-admin-default")
          VENV="$VENV_BASE/$REQ_HASH"
          if [ ! -f "$VENV/bin/python" ]; then
            mkdir -p "$VENV_BASE"
            /usr/bin/python3 -m venv "$VENV"
            "$VENV/bin/pip" install --upgrade pip --quiet
          fi
          "$VENV/bin/pip" install pyyaml --quiet
          echo "$VENV/bin" >> $GITHUB_PATH

      - name: Validate Snapshot Admin Workflow
        run: |
          echo "[smoke] validate workflow only (no web UI, no deploy)"
          python3 tools/validate_snapshot_admin_workflow_v1.py

      - name: Validate DB First Pipeline
        run: |
          echo "[smoke] validate DB-first pipeline contract"
          python3 tools/validate_db_first_pipeline_v1.py

  # Manual dispatch gate: full workflow + web UI validation only.
  validate-snapshot-admin-full:
    if: github.event_name == 'workflow_dispatch'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        run: |
          echo "[full] workflow_dispatch snapshot admin validation"
          if [ -d .git ]; then
            git remote set-url origin http://x-access-token:${{ secrets.GITHUB_TOKEN }}@192.168.123.100:8418/KimJaeHyun/myfinance.git
          else
            git init
            git remote add origin http://x-access-token:${{ secrets.GITHUB_TOKEN }}@192.168.123.100:8418/KimJaeHyun/myfinance.git
          fi
          git fetch origin main --depth=1
          git reset --hard FETCH_HEAD

      - name: Setup Python Environment
        run: |
          echo "[full] prepare python venv"
          VENV_BASE=/volume1/gitea/python_venv
          REQ_HASH=$(md5sum tools/validate_snapshot_admin_workflow_v1.py 2>/dev/null | cut -d' ' -f1 || echo "snapshot-admin-default")
          VENV="$VENV_BASE/$REQ_HASH"
          if [ ! -f "$VENV/bin/python" ]; then
            mkdir -p "$VENV_BASE"
            /usr/bin/python3 -m venv "$VENV"
            "$VENV/bin/pip" install --upgrade pip --quiet
          fi
          "$VENV/bin/pip" install pyyaml --quiet
          echo "$VENV/bin" >> $GITHUB_PATH

      - name: Validate Snapshot Admin Workflow
        run: |
          echo "[full] validate workflow"
          python3 tools/validate_snapshot_admin_workflow_v1.py

      - name: Validate DB First Pipeline
        run: |
          echo "[full] validate DB-first pipeline contract"
          python3 tools/validate_db_first_pipeline_v1.py

      - name: Validate Snapshot Admin Web UI
        run: |
          echo "[full] validate web ui"
          python3 tools/validate_snapshot_admin_web_v1.py

      - name: Notify Run Result
        if: always()
        run: |
          STATUS="${{ job.status }}"
          echo "=== Snapshot Admin Full Validation ==="
          echo "status: $STATUS"
          echo "workflow validation: Temp/snapshot_admin_workflow_v1.json"
          echo "web validation: Temp/snapshot_admin_web_validation_v1.json"
