#!/usr/bin/env pwsh
# GM-Relay Solution Initialization Script
# Requires: .NET 10 SDK

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$root = $PSScriptRoot

Write-Host ""
Write-Host "[GM-Relay] Initializing solution..." -ForegroundColor Cyan
Write-Host ""

# --- Prerequisites ---
$sdkVersion = dotnet --version
Write-Host "  .NET SDK: $sdkVersion" -ForegroundColor Gray

Write-Host "[1/8] Installing Aspire project templates..." -ForegroundColor Yellow
dotnet new install Aspire.ProjectTemplates

# --- Solution ---
Write-Host "[2/8] Creating solution..." -ForegroundColor Yellow
dotnet new sln --name GM-Relay --output $root --force

# --- Projects ---
Write-Host "[3/8] Creating projects..." -ForegroundColor Yellow
dotnet new aspire-apphost -n GmRelay.AppHost -o "$root/src/GmRelay.AppHost" --force
dotnet new aspire-servicedefaults -n GmRelay.ServiceDefaults -o "$root/src/GmRelay.ServiceDefaults" --force
dotnet new worker -n GmRelay.Bot -o "$root/src/GmRelay.Bot" --force
dotnet new xunit -n GmRelay.Bot.Tests -o "$root/tests/GmRelay.Bot.Tests" --force

# --- Add to solution ---
Write-Host "[4/8] Adding projects to solution..." -ForegroundColor Yellow
dotnet sln "$root/GM-Relay.sln" add `
    "$root/src/GmRelay.AppHost/GmRelay.AppHost.csproj" `
    "$root/src/GmRelay.ServiceDefaults/GmRelay.ServiceDefaults.csproj" `
    "$root/src/GmRelay.Bot/GmRelay.Bot.csproj" `
    "$root/tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj"

# --- Project references ---
Write-Host "[5/8] Setting up project references..." -ForegroundColor Yellow
dotnet add "$root/src/GmRelay.Bot/GmRelay.Bot.csproj" reference "$root/src/GmRelay.ServiceDefaults/GmRelay.ServiceDefaults.csproj"
dotnet add "$root/src/GmRelay.AppHost/GmRelay.AppHost.csproj" reference "$root/src/GmRelay.Bot/GmRelay.Bot.csproj"
dotnet add "$root/tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj" reference "$root/src/GmRelay.Bot/GmRelay.Bot.csproj"

# --- NuGet: Bot ---
Write-Host "[6/8] Installing NuGet packages (Bot)..." -ForegroundColor Yellow
$botCsproj = "$root/src/GmRelay.Bot/GmRelay.Bot.csproj"
dotnet add $botCsproj package Telegram.Bot
dotnet add $botCsproj package Npgsql
dotnet add $botCsproj package Dapper
dotnet add $botCsproj package Dapper.AOT
dotnet add $botCsproj package dbup-postgresql
dotnet add $botCsproj package Aspire.Npgsql

# --- NuGet: AppHost ---
Write-Host "[7/8] Installing NuGet packages (AppHost)..." -ForegroundColor Yellow
dotnet add "$root/src/GmRelay.AppHost/GmRelay.AppHost.csproj" package Aspire.Hosting.PostgreSQL

# --- Cleanup template files ---
$workerFile = "$root/src/GmRelay.Bot/Worker.cs"
if (Test-Path $workerFile) {
    Remove-Item $workerFile -Force
    Write-Host "  Removed template Worker.cs" -ForegroundColor Gray
}

# --- Create directory structure ---
Write-Host "[8/8] Creating directory structure..." -ForegroundColor Yellow
$dirs = @(
    "src/GmRelay.Bot/Infrastructure/Database"
    "src/GmRelay.Bot/Infrastructure/Telegram"
    "src/GmRelay.Bot/Infrastructure/Scheduling"
    "src/GmRelay.Bot/Domain"
    "src/GmRelay.Bot/Migrations"
    "src/GmRelay.Bot/Features/Sessions/CreateSession"
    "src/GmRelay.Bot/Features/Confirmation/SendConfirmation"
    "src/GmRelay.Bot/Features/Confirmation/HandleRsvp"
    "src/GmRelay.Bot/Features/Reminders/SendJoinLink"
    "tests/GmRelay.Bot.Tests/Features/Confirmation"
    "docs/adr"
)

foreach ($dir in $dirs) {
    $fullPath = Join-Path $root $dir
    if (-not (Test-Path $fullPath)) {
        New-Item -ItemType Directory -Path $fullPath -Force | Out-Null
    }
}

Write-Host ""
Write-Host "[OK] GM-Relay solution initialized!" -ForegroundColor Green
Write-Host "     Run: dotnet build GM-Relay.sln" -ForegroundColor Gray
Write-Host ""
