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 = ""
$webJobGitLab = ""
if ($Platform -match "Web|Both") {
    $installCmd = if ($Platform -eq "Both") { "npm ci" } else { "cd frontend && npm ci" }
    $buildCmd = if ($Platform -eq "Both") { "npm run build --workspace=apps/web" } else { "cd frontend && npm run build" }
    
    $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: $installCmd
    - name: Build
      run: $buildCmd
"@


    $gitlabInstall = if ($Platform -eq "Both") { "npm ci" } else { "cd frontend && npm ci" }
    $gitlabBuild = if ($Platform -eq "Both") { "npm run build --workspace=apps/web" } else { "cd frontend && npm run build" }
    $webJobGitLab = @"
web-build:
  stage: test
  image: node:20-alpine
  script:
    - $gitlabInstall
    - $gitlabBuild
"@

}

$mobileJobGitHub = ""
$mobileJobGitLab = ""
if ($Platform -match "Mobile|Both") {
    $installCmd = if ($Platform -eq "Both") { "npm ci" } else { "cd mobile && npm ci" }
    $checkCmd = if ($Platform -eq "Both") { "npx tsc --noEmit --workspace=apps/mobile" } else { "cd mobile && npx tsc --noEmit" }

    $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: $installCmd
    - name: Type check
      run: $checkCmd
"@


    $gitlabInstall = if ($Platform -eq "Both") { "npm ci" } else { "cd mobile && npm ci" }
    $gitlabCheck = if ($Platform -eq "Both") { "npx tsc --noEmit --workspace=apps/mobile" } else { "cd mobile && npx tsc --noEmit" }
    $mobileJobGitLab = @"
mobile-lint:
  stage: test
  image: node:20-alpine
  script:
    - $gitlabInstall
    - $gitlabCheck
"@

}

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: 8.0.x
    - name: Restore dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --no-restore
    - name: Test
      run: dotnet test --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: docker compose build
"@


    if ($Global:DryRun) {
        Write-Host " [PLAN] Write ci.yml and docker.yml" -ForegroundColor DarkGray
    } else {
        [System.IO.File]::WriteAllText((Join-Path $CiDir "ci.yml"), $ciYml)
        [System.IO.File]::WriteAllText((Join-Path $CiDir "docker.yml"), $dockerYml)
    }
}
elseif ($CiProvider -eq "GitLab") {
    $ciFile = Join-Path $OutputPath ".gitlab-ci.yml"
    $gitlabYml = @"
image: mcr.microsoft.com/dotnet/sdk:8.0
 
stages:
  - build
  - test
 
backend-build:
  stage: build
  script:
    - "dotnet restore"
    - "dotnet build --no-restore"
    - "dotnet test --no-build --verbosity normal"
"@


    if ($webJobGitLab) { $gitlabYml += "`n`n" + $webJobGitLab }
    if ($mobileJobGitLab) { $gitlabYml += "`n`n" + $mobileJobGitLab }

    if ($Global:DryRun) {
        Write-Host " [PLAN] Write .gitlab-ci.yml" -ForegroundColor DarkGray
    } else {
        [System.IO.File]::WriteAllText($ciFile, $gitlabYml)
    }
}