modules/devops/Write-IdeConfig.ps1

param(
    [string]$ProjectName,
    [string]$OutputPath,
    [string]$IdeConfig,
    [string]$Platform = "Web"
)

if ($IdeConfig -ne "VSCode") { return }

. (Join-Path (Split-Path $MyInvocation.MyCommand.Path) "..\core\Utils.ps1")
$base = Join-Path $OutputPath ".vscode"

function Write-File($rel, $con) { Write-CoreFile (Join-Path $base $rel) $con }
function T($t) { $t.Replace("{P}", $ProjectName) }

# 1. launch.json
Write-File "launch.json" (T @'
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (WebApi)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/src/{P}.WebApi/bin/Debug/net8.0/{P}.WebApi.dll",
            "args": [],
            "cwd": "${workspaceFolder}/src/{P}.WebApi",
            "stopAtEntry": false,
            "serverReadyAction": {
                "action": "openExternally",
                "pattern": "\\bNow listening on:\\s+(https?://\\S+)"
            },
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            },
            "sourceFileMap": {
                "/Views": "${workspaceFolder}/Views"
            }
        }
    ]
}
'@
)

# 2. tasks.json (Build tasks dynamically)
$tasksList = @(
    @{
        label = "build"
        command = "dotnet"
        type = "process"
        args = @(
            "build",
            "${workspaceFolder}/$ProjectName.sln",
            "/property:GenerateFullPaths=true",
            "/consoleloggerparameters:NoSummary"
        )
        problemMatcher = "$msCompile"
    },
    @{
        label = "docker-up"
        type = "shell"
        command = "docker compose up -d --build"
        presentation = @{
            reveal = "always"
            panel = "new"
        }
    },
    @{
        label = "docker-down"
        type = "shell"
        command = "docker compose down"
        presentation = @{
            reveal = "always"
        }
    }
)

if ($Platform -eq "Web") {
    $tasksList += @{
        label = "npm: dev (web)"
        type = "shell"
        command = "npm run dev"
        options = @{
            cwd = "${workspaceFolder}/frontend"
        }
        presentation = @{
            reveal = "always"
        }
    }
} elseif ($Platform -eq "Mobile") {
    $tasksList += @{
        label = "expo: start"
        type = "shell"
        command = "npm start"
        options = @{
            cwd = "${workspaceFolder}/mobile"
        }
        presentation = @{
            reveal = "always"
        }
    }
} elseif ($Platform -eq "Both") {
    $tasksList += @{
        label = "npm: dev (web)"
        type = "shell"
        command = "npm run dev --workspace=apps/web"
        presentation = @{
            reveal = "always"
        }
    }
    $tasksList += @{
        label = "expo: start"
        type = "shell"
        command = "npm start --workspace=apps/mobile"
        presentation = @{
            reveal = "always"
        }
    }
}

$tasksJson = @{
    version = "2.0.0"
    tasks = $tasksList
} | ConvertTo-Json -Depth 10

Write-File "tasks.json" $tasksJson

# 3. extensions.json
$recs = @(
    "ms-dotnettools.csharp",
    "ms-dotnettools.vscode-dotnet-runtime",
    "ms-azuretools.vscode-docker",
    "dbaeumer.vscode-eslint",
    "esbenp.prettier-vscode"
)

if ($Platform -match "Mobile|Both") {
    $recs += "expo.vscode-expo-tools"
}

$extensionsJson = @{
    recommendations = $recs
} | ConvertTo-Json -Depth 10

Write-File "extensions.json" $extensionsJson