# Test script for ColaFlow Project Management API
# Day 12 - Complete CRUD + Multi-Tenant + SignalR Integration

$baseUrl = "http://localhost:5167"
$ErrorActionPreference = "Continue"

Write-Host "========================================"  -ForegroundColor Cyan
Write-Host "ColaFlow Project API Test" -ForegroundColor Cyan
Write-Host "========================================"  -ForegroundColor Cyan
Write-Host ""

# Step 1: Register a new tenant and get access token
Write-Host "[1] Registering new tenant..." -ForegroundColor Yellow
$tenantSlug = "test-project-corp-$(Get-Random -Minimum 1000 -Maximum 9999)"
$registerBody = @{
    tenantName = "Test Project Corp"
    tenantSlug = $tenantSlug
    subscriptionPlan = "Professional"
    adminEmail = "admin@$tenantSlug.com"
    adminPassword = "Admin@1234"
    adminFullName = "Project Admin"
} | ConvertTo-Json

try {
    $registerResponse = Invoke-RestMethod -Uri "$baseUrl/api/tenants/register" `
        -Method Post `
        -ContentType "application/json" `
        -Body $registerBody

    $token = $registerResponse.accessToken
    $tenantId = $registerResponse.tenant.id
    $userId = $registerResponse.user.id

    Write-Host "[SUCCESS] Tenant registered" -ForegroundColor Green
    Write-Host "  Tenant ID: $tenantId" -ForegroundColor Gray
    Write-Host "  User ID: $userId" -ForegroundColor Gray
    Write-Host ""
} catch {
    Write-Host "[FAILED] Failed to register tenant" -ForegroundColor Red
    Write-Host $_.Exception.Message -ForegroundColor Red
    exit 1
}

$headers = @{
    "Authorization" = "Bearer $token"
    "Content-Type" = "application/json"
}

# Step 2: Create a project
Write-Host "[2] Creating project..." -ForegroundColor Yellow
$projectKey = "PRJ$(Get-Random -Minimum 100 -Maximum 999)"
$createProjectBody = @{
    name = "ColaFlow v2.0"
    description = "Next generation project management system with AI integration"
    key = $projectKey
} | ConvertTo-Json

try {
    $project = Invoke-RestMethod -Uri "$baseUrl/api/v1/projects" `
        -Method Post `
        -Headers $headers `
        -Body $createProjectBody

    $projectId = $project.id

    Write-Host "[SUCCESS] Project created" -ForegroundColor Green
    Write-Host "  Project ID: $projectId" -ForegroundColor Gray
    Write-Host "  Name: $($project.name)" -ForegroundColor Gray
    Write-Host "  Key: $($project.key)" -ForegroundColor Gray
    Write-Host "  Status: $($project.status)" -ForegroundColor Gray
    Write-Host ""
} catch {
    Write-Host "[FAILED] Failed to create project" -ForegroundColor Red
    Write-Host $_.Exception.Message -ForegroundColor Red
    exit 1
}

# Step 3: Get all projects
Write-Host "[3] Listing all projects..." -ForegroundColor Yellow
try {
    $projects = Invoke-RestMethod -Uri "$baseUrl/api/v1/projects" `
        -Method Get `
        -Headers $headers

    Write-Host "[SUCCESS] Retrieved projects" -ForegroundColor Green
    Write-Host "  Total projects: $($projects.Count)" -ForegroundColor Gray
    Write-Host ""
} catch {
    Write-Host "[FAILED] Failed to list projects" -ForegroundColor Red
    exit 1
}

# Step 4: Get specific project by ID
Write-Host "[4] Getting project by ID..." -ForegroundColor Yellow
try {
    $retrievedProject = Invoke-RestMethod -Uri "$baseUrl/api/v1/projects/$projectId" `
        -Method Get `
        -Headers $headers

    Write-Host "[SUCCESS] Retrieved project" -ForegroundColor Green
    Write-Host "  Name: $($retrievedProject.name)" -ForegroundColor Gray
    Write-Host ""
} catch {
    Write-Host "[FAILED] Failed to get project" -ForegroundColor Red
    exit 1
}

# Step 5: Update project
Write-Host "[5] Updating project..." -ForegroundColor Yellow
$updateProjectBody = @{
    name = "ColaFlow v2.0 - Updated"
    description = "Next generation project management system - Enhanced"
} | ConvertTo-Json

try {
    $updatedProject = Invoke-RestMethod -Uri "$baseUrl/api/v1/projects/$projectId" `
        -Method Put `
        -Headers $headers `
        -Body $updateProjectBody

    Write-Host "[SUCCESS] Project updated" -ForegroundColor Green
    Write-Host "  New Name: $($updatedProject.name)" -ForegroundColor Gray
    Write-Host ""
} catch {
    Write-Host "[FAILED] Failed to update project" -ForegroundColor Red
    exit 1
}

# Step 6: Create another project
Write-Host "[6] Creating second project..." -ForegroundColor Yellow
$projectKey2 = "TOOL$(Get-Random -Minimum 100 -Maximum 999)"
$createProject2Body = @{
    name = "Internal Tools"
    description = "Internal tooling and automation"
    key = $projectKey2
} | ConvertTo-Json

try {
    $project2 = Invoke-RestMethod -Uri "$baseUrl/api/v1/projects" `
        -Method Post `
        -Headers $headers `
        -Body $createProject2Body

    Write-Host "[SUCCESS] Second project created" -ForegroundColor Green
    Write-Host "  Name: $($project2.name)" -ForegroundColor Gray
    Write-Host ""
} catch {
    Write-Host "[FAILED] Failed to create second project" -ForegroundColor Red
    exit 1
}

# Step 7: Verify both projects are visible
Write-Host "[7] Verifying tenant isolation..." -ForegroundColor Yellow
try {
    $allProjects = Invoke-RestMethod -Uri "$baseUrl/api/v1/projects" `
        -Method Get `
        -Headers $headers

    Write-Host "[SUCCESS] Retrieved all tenant projects" -ForegroundColor Green
    Write-Host "  Total projects: $($allProjects.Count)" -ForegroundColor Gray

    if ($allProjects.Count -eq 2) {
        Write-Host "  [OK] Multi-tenant isolation working" -ForegroundColor Green
    }
    Write-Host ""
} catch {
    Write-Host "[FAILED] Failed to verify tenant isolation" -ForegroundColor Red
    exit 1
}

# Step 8: Archive first project
Write-Host "[8] Archiving project..." -ForegroundColor Yellow
try {
    Invoke-RestMethod -Uri "$baseUrl/api/v1/projects/$projectId" `
        -Method Delete `
        -Headers $headers

    Write-Host "[SUCCESS] Project archived" -ForegroundColor Green
    Write-Host ""
} catch {
    Write-Host "[FAILED] Failed to archive project" -ForegroundColor Red
    exit 1
}

# Step 9: Verify archived project
Write-Host "[9] Verifying project archival..." -ForegroundColor Yellow
try {
    $archivedProject = Invoke-RestMethod -Uri "$baseUrl/api/v1/projects/$projectId" `
        -Method Get `
        -Headers $headers

    Write-Host "[SUCCESS] Retrieved archived project" -ForegroundColor Green
    Write-Host "  Status: $($archivedProject.status)" -ForegroundColor Gray

    if ($archivedProject.status -eq "Archived") {
        Write-Host "  [OK] Project successfully archived" -ForegroundColor Green
    }
    Write-Host ""
} catch {
    Write-Host "[FAILED] Failed to verify archival" -ForegroundColor Red
    exit 1
}

# Summary
Write-Host "========================================"  -ForegroundColor Cyan
Write-Host "Test Summary" -ForegroundColor Cyan
Write-Host "========================================"  -ForegroundColor Cyan
Write-Host ""
Write-Host "All tests passed successfully!" -ForegroundColor Green
Write-Host ""
Write-Host "Implemented Features:" -ForegroundColor Cyan
Write-Host "  - Complete CRUD operations" -ForegroundColor Green
Write-Host "  - Multi-tenant isolation with Global Query Filter" -ForegroundColor Green
Write-Host "  - JWT-based authorization" -ForegroundColor Green
Write-Host "  - Domain Events (ProjectCreated, Updated, Archived)" -ForegroundColor Green
Write-Host "  - SignalR integration ready" -ForegroundColor Green
Write-Host ""
Write-Host "Project Management Module - Day 12 Complete!" -ForegroundColor Green
Write-Host "========================================"  -ForegroundColor Cyan
