modules/solution/Initialize-Solution.ps1

param(
    [string]$ProjectName,
    [string]$OrmMode,
    [string]$OutputPath,
    [bool]$SkipTests = $false
)

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

$src   = Join-Path $OutputPath "src"
$tests = Join-Path $OutputPath "tests"

if (-not $Global:DryRun) {
    New-Item -ItemType Directory -Path $src -Force | Out-Null
    if (-not $SkipTests) { New-Item -ItemType Directory -Path $tests -Force | Out-Null }
}

# --- Solution ---
if (-not $Global:DryRun) { Push-Location $OutputPath }
Invoke-Cmd "dotnet" "new sln -n $ProjectName --format sln --force"
if (-not $Global:DryRun) { Pop-Location }

# --- Proje Yaratma (Paralel) ---
$jobs = @()
$allLayers = @("Domain", "Application", "Infrastructure", "Persistence")
foreach ($layer in $allLayers) {
    $pName = "$ProjectName.$layer"
    $pDir  = Join-Path $src $pName
    if ($Global:DryRun) {
        Invoke-Cmd "dotnet" "new classlib -n $pName -o $pDir"
    } else {
        $jobs += Start-Job -ScriptBlock { param($n, $d) dotnet new classlib -n $n -o $d --framework net8.0 --force } -ArgumentList $pName, $pDir
    }
}

$webapiName = "$ProjectName.WebApi"
$webapiDir  = Join-Path $src $webapiName
if ($Global:DryRun) {
    Invoke-Cmd "dotnet" "new webapi -n $webapiName -o $webapiDir"
} else {
    $jobs += Start-Job -ScriptBlock { param($n, $d) dotnet new webapi -n $n -o $d --framework net8.0 --use-controllers --force } -ArgumentList $webapiName, $webapiDir
}

if (-not $SkipTests) {
    foreach ($t in @("Domain.Tests", "Application.Tests", "Integration.Tests")) {
        $tName = "$ProjectName.$t"
        $tDir = Join-Path $tests $tName
        if ($Global:DryRun) {
            Invoke-Cmd "dotnet" "new xunit -n $tName -o $tDir"
        } else {
            $jobs += Start-Job -ScriptBlock { param($n, $d) dotnet new xunit -n $n -o $d --framework net8.0 --force } -ArgumentList $tName, $tDir
        }
    }
}

if (-not $Global:DryRun) {
    $jobs | Wait-Job | Out-Null
    $jobs | Remove-Job

    # dotnet new classlib/xunit'in otomatik olusturdugu Class1.cs dosyalarini sil
    foreach ($layer in $allLayers) {
        $class1 = Join-Path $src "$ProjectName.$layer\Class1.cs"
        if (Test-Path $class1) { Remove-Item $class1 -Force }
    }
    if (-not $SkipTests) {
        foreach ($t in @("Domain.Tests", "Application.Tests", "Integration.Tests")) {
            $uc1 = Join-Path $tests "$ProjectName.$t\UnitTest1.cs"
            if (Test-Path $uc1) { Remove-Item $uc1 -Force }
        }
    }

    # Remove WeatherForecast template files from dotnet new webapi
    $weatherFiles = @(
        (Join-Path $src "$webapiName\Controllers\WeatherForecastController.cs"),
        (Join-Path $src "$webapiName\WeatherForecast.cs")
    )
    foreach ($wf in $weatherFiles) {
        if (Test-Path $wf) { Remove-Item $wf -Force }
    }
}

# --- Solution'a Ekle (Toplu) ---
$allProjPaths = @()
# WebApi projesini listenin en basina alarak Visual Studio/Rider gibi IDE'lerin bunu varsayilan baslangic projesi (startup project) yapmasini sagliyoruz.
$allProjPaths += "`"src\$ProjectName.WebApi\$ProjectName.WebApi.csproj`""
foreach ($l in $allLayers) { $allProjPaths += "`"src\$ProjectName.$l\$ProjectName.$l.csproj`"" }
if (-not $SkipTests) {
    foreach ($t in @("Domain.Tests", "Application.Tests", "Integration.Tests")) { $allProjPaths += "`"tests\$ProjectName.$t\$ProjectName.$t.csproj`"" }
}

if (-not $Global:DryRun) { Push-Location $OutputPath }
Invoke-Cmd "dotnet" "sln `"$ProjectName.sln`" add $($allProjPaths -join ' ')"
if (-not $Global:DryRun) { Pop-Location }

# --- Proje referanslari ---
$domainCsproj  = Join-Path $src "$ProjectName.Domain\$ProjectName.Domain.csproj"
$appCsproj     = Join-Path $src "$ProjectName.Application\$ProjectName.Application.csproj"
$infraCsproj   = Join-Path $src "$ProjectName.Infrastructure\$ProjectName.Infrastructure.csproj"
$persCsproj    = Join-Path $src "$ProjectName.Persistence\$ProjectName.Persistence.csproj"
$webapiCsproj  = Join-Path $src "$ProjectName.WebApi\$ProjectName.WebApi.csproj"

Invoke-Cmd "dotnet" "add $appCsproj reference $domainCsproj"
Invoke-Cmd "dotnet" "add $infraCsproj reference $appCsproj"
Invoke-Cmd "dotnet" "add $persCsproj reference $domainCsproj"
Invoke-Cmd "dotnet" "add $persCsproj reference $appCsproj"
Invoke-Cmd "dotnet" "add $webapiCsproj reference $appCsproj"
Invoke-Cmd "dotnet" "add $webapiCsproj reference $infraCsproj"
Invoke-Cmd "dotnet" "add $webapiCsproj reference $persCsproj"

if (-not $SkipTests) {
    $domainTestCsproj = Join-Path $tests "$ProjectName.Domain.Tests\$ProjectName.Domain.Tests.csproj"
    $appTestCsproj    = Join-Path $tests "$ProjectName.Application.Tests\$ProjectName.Application.Tests.csproj"
    $integTestCsproj  = Join-Path $tests "$ProjectName.Integration.Tests\$ProjectName.Integration.Tests.csproj"

    Invoke-Cmd "dotnet" "add $domainTestCsproj reference $domainCsproj"
    Invoke-Cmd "dotnet" "add $appTestCsproj reference $appCsproj"
    Invoke-Cmd "dotnet" "add $integTestCsproj reference $webapiCsproj"
}