<#
.SYNOPSIS
    Sets up GitHub Secrets and Bicep parameters from config.ps1 (project-wide).

.DESCRIPTION
    Reads config.ps1 (not committed) in the repo root and applies values to:
      -GitHub   GitHub repository secrets via gh CLI (webservice + foundry)
      -Bicep    Generates both infra/main.local.bicepparam files:
                  - webservice/infra/main.local.bicepparam
                  - foundry-agent/infra/main.local.bicepparam
      -All      Both targets

.EXAMPLE
    .\setup.ps1 -All
    .\setup.ps1 -GitHub      # GitHub Secrets only
    .\setup.ps1 -Bicep       # Generate bicepparam files only

.NOTES
    Requires: GitHub CLI (gh) for -GitHub flag.
    Install:  winget install GitHub.cli
#>
param(
    [switch]$GitHub,
    [switch]$Bicep,
    [switch]$All
)

#---------------------------------------------------------------------------
# Load config
#---------------------------------------------------------------------------
$configPath = Join-Path $PSScriptRoot "config.ps1"
if (-not (Test-Path $configPath)) {
    Write-Host "config.ps1 not found." -ForegroundColor Red
    Write-Host "Copy config.example.ps1 to config.ps1 and fill in your values." -ForegroundColor Yellow
    exit 1
}
. $configPath

#---------------------------------------------------------------------------
# 1. GitHub Secrets via gh CLI
#---------------------------------------------------------------------------
if ($GitHub -or $All) {
    Write-Host "Setting GitHub Secrets..." -ForegroundColor Yellow

    # Shared / webservice
    gh secret set AZURE_CLIENT_ID          --body $config.AzureClientId
    gh secret set AZURE_SUBSCRIPTION_ID    --body $config.SubscriptionId
    gh secret set AZURE_TENANT_ID          --body $config.TenantId
    gh secret set AZURE_RESOURCE_GROUP     --body $config.ResourceGroup
    gh secret set AZURE_LOCATION           --body $config.Location
    gh secret set APP_NAME                 --body $config.AppName
    gh secret set APP_SERVICE_PLAN_NAME    --body $config.AppServicePlanName
    gh secret set APP_INSIGHTS_NAME        --body $config.AppInsightsName
    gh secret set APP_CUSTOM_DOMAIN        --body $config.CustomDomain
    if ($config.OAuthTokenSecret) {
        gh secret set OAUTH_TOKEN_SECRET   --body $config.OAuthTokenSecret
    }
    if ($config.StandardLibraryAthleteId) {
        gh secret set STANDARD_LIBRARY_ATHLETE_ID --body $config.StandardLibraryAthleteId
    }
    if ($config.McpTraceResponseJson) {
        gh secret set MCP_TRACE_RESPONSE_JSON --body $config.McpTraceResponseJson
    }
    if ($config.McpTraceResponsePreviewLimit) {
        gh secret set MCP_TRACE_RESPONSE_PREVIEW_LIMIT --body $config.McpTraceResponsePreviewLimit
    }
    if ($config.McpRpcEventLogLevel) {
        gh secret set MCP_RPC_EVENT_LOG_LEVEL --body $config.McpRpcEventLogLevel
    }

    # Foundry agent
    if ($config.FoundryProjectEndpoint) {
        gh secret set FOUNDRY_PROJECT_ENDPOINT --body $config.FoundryProjectEndpoint
    }
    if ($config.FoundryResourceGroup) {
        gh secret set FOUNDRY_RESOURCE_GROUP --body $config.FoundryResourceGroup
    }
    if ($config.FoundryAccountName) {
        gh secret set FOUNDRY_ACCOUNT_NAME --body $config.FoundryAccountName
    }
    if ($config.FoundryProjectName) {
        gh secret set FOUNDRY_PROJECT_NAME --body $config.FoundryProjectName
    }
    if ($config.FoundryLocation) {
        gh secret set FOUNDRY_LOCATION --body $config.FoundryLocation
    }
    if ($config.FoundryModelVersion) {
        gh secret set FOUNDRY_MODEL_VERSION --body $config.FoundryModelVersion
    }
    if ($config.FoundryDeployPrincipalId) {
        gh secret set FOUNDRY_DEPLOY_PRINCIPAL_ID --body $config.FoundryDeployPrincipalId
    }

    Write-Host "GitHub Secrets set." -ForegroundColor Green
}

#---------------------------------------------------------------------------
# 2. Generate bicepparam files
#---------------------------------------------------------------------------
if ($Bicep -or $All) {
    # --- webservice/infra/main.local.bicepparam ---------------------------
    Write-Host "Generating webservice/infra/main.local.bicepparam..." -ForegroundColor Yellow

    $webContent = @"
//
// main.local.bicepparam — generated by setup.ps1, do not commit
//
using './main.bicep'

param appName            = '$($config.AppName)'
param location           = '$($config.Location)'
param appServicePlanName = '$($config.AppServicePlanName)'
param appInsightsName    = '$($config.AppInsightsName)'
param customDomain       = '$($config.CustomDomain)'
param standardLibraryAthleteId = '$($config.StandardLibraryAthleteId)'
param mcpTraceResponseJson = '$($config.McpTraceResponseJson)'
param mcpTraceResponsePreviewLimit = '$($config.McpTraceResponsePreviewLimit)'
param mcpRpcEventLogLevel = '$($config.McpRpcEventLogLevel)'
"@

    $webOut = Join-Path $PSScriptRoot "webservice/infra/main.local.bicepparam"
    Set-Content -Path $webOut -Value $webContent -Encoding UTF8
    Write-Host "webservice/infra/main.local.bicepparam generated." -ForegroundColor Green

    # --- foundry-agent/infra/main.local.bicepparam ------------------------
    Write-Host "Generating foundry-agent/infra/main.local.bicepparam..." -ForegroundColor Yellow

    $foundryContent = @"
//
// main.local.bicepparam — generated by setup.ps1, do not commit
//
using './main.bicep'

param foundryAccountName = '$($config.FoundryAccountName)'
param foundryProjectName = '$($config.FoundryProjectName)'
param location           = '$($config.FoundryLocation)'
param modelVersion       = '$($config.FoundryModelVersion)'
param deployPrincipalId  = '$($config.FoundryDeployPrincipalId)'
"@

    $foundryOut = Join-Path $PSScriptRoot "foundry-agent/infra/main.local.bicepparam"
    Set-Content -Path $foundryOut -Value $foundryContent -Encoding UTF8
    Write-Host "foundry-agent/infra/main.local.bicepparam generated." -ForegroundColor Green
}

Write-Host "Done." -ForegroundColor Green
