modules/devops/Write-CiPipeline.ps1

param(
    [string]$ProjectName,
    [string]$OutputPath,
    [string]$CiProvider = "GitHub",
    [string]$Platform = "Web",
    [string]$FrontendMode = "ReactTS"
)

. (Join-Path (Split-Path $MyInvocation.MyCommand.Path) "..\core\Utils.ps1")

if ($CiProvider -eq "None") { return }

# --- Build Web and Mobile jobs dynamically ---
$webJobGitHub = ""
if ($Platform -match "Web|Both") {
    $webJobGitHub = @"
  web-build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: 20
    - name: Install dependencies
      run: cd $ProjectName-web && npm install
    - name: Build
      run: cd $ProjectName-web && npm run build
"@

}

$mobileJobGitHub = ""
if ($Platform -match "Mobile|Both") {
    $mobileJobGitHub = @"
  mobile-lint:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: 20
    - name: Install dependencies
      run: cd $ProjectName-mobile && npm install
    - name: Type check
      run: cd $ProjectName-mobile && npx tsc --noEmit
"@

}

if ($CiProvider -eq "GitHub") {
    $CiDir = Join-Path $OutputPath ".github\workflows"
    if ($Global:DryRun) {
        Write-Host " [PLAN] Create GitHub Actions at $CiDir" -ForegroundColor DarkGray
    } else {
        New-Item -ItemType Directory -Path $CiDir -Force | Out-Null
    }

    # ci.yml
    $ciYml = @"
name: CI
 
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
 
jobs:
  backend-build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Setup .NET
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: 10.0.x
    - name: Restore dependencies
      run: dotnet restore $ProjectName-backend/$ProjectName.sln
    - name: Build
      run: dotnet build $ProjectName-backend/$ProjectName.sln --no-restore
    - name: Test
      run: dotnet test $ProjectName-backend/$ProjectName.sln --no-build --verbosity normal
"@


    if ($webJobGitHub) { $ciYml += "`n`n" + $webJobGitHub }
    if ($mobileJobGitHub) { $ciYml += "`n`n" + $mobileJobGitHub }

    # docker.yml
    $dockerYml = @"
name: Docker Build Check
 
on:
  push:
    branches: [ main ]
 
jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Build the stack
      run: cd $ProjectName-docker && docker compose build
"@


    if ($Global:DryRun) {
        Write-Host " [PLAN] Write ci.yml and docker.yml" -ForegroundColor DarkGray
    } else {
        Write-CoreFile (Join-Path $CiDir "ci.yml") $ciYml
        Write-CoreFile (Join-Path $CiDir "docker.yml") $dockerYml
    }
}