name: iOS Build & TestFlight

on:
  push:
    tags:
      - 'v*' # 例: git tag v1.0.0 をプッシュした時に実行

jobs:
  build:
    name: Build & Deploy to TestFlight
    runs-on: macos-latest
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      
      - name: Set up Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.29.x'
          channel: 'stable'
          
      - name: Install dependencies
        run: flutter pub get
        
      # iOSビルド (証明書不要の No Code Sign オプション)
      # 実際の署名はAppStore Connectへのアップロード時に行われる
      - name: Build iOS
        run: |
          flutter build ios --release --no-codesign \
            --dart-define=GEMINI_API_KEY=dist-build-key \
            --dart-define=AI_PROXY_URL=${{ secrets.VPS_PROXY_URL }} \
            --dart-define=USE_PROXY=true
            
      # TestFlight へのアップロード
      # App Store Connect API Key を GitHub Secrets に設定する必要あり
      - name: Upload to TestFlight
        uses: apple-actions/upload-testflight-build@v1
        with:
          app-path: 'build/ios/iphoneos/Runner.app'
          issuer-id: ${{ secrets.APPSTORE_ISSUER_ID }}
          api-key-id: ${{ secrets.APPSTORE_API_KEY_ID }}
          api-private-key: ${{ secrets.APPSTORE_API_PRIVATE_KEY }}
