# PowerShell Script zum Erstellen der Products Collection
# Erstellt die Collection mit allen erforderlichen Attributen

$ErrorActionPreference = "Continue"

Write-Host "=== Products Collection Setup ===" -ForegroundColor Cyan
Write-Host ""

# Konfiguration
$DATABASE_ID = "eship-db"
$COLLECTION_ID = "products"
$COLLECTION_NAME = "Products"

# 1. Prüfe Login-Status
Write-Host "1. Prüfe Appwrite Login-Status..." -ForegroundColor Yellow
$loginCheck = appwrite databases list 2>&1

if ($LASTEXITCODE -ne 0 -and $loginCheck -like "*Session not found*") {
    Write-Host "   [WARNUNG] Nicht eingeloggt. Bitte zuerst einloggen:" -ForegroundColor Red
    Write-Host "   appwrite login" -ForegroundColor White
    Write-Host ""
    Write-Host "   Führe diesen Befehl jetzt aus..." -ForegroundColor Yellow
    appwrite login
    Write-Host ""
}

# 2. Erstelle Collection
Write-Host "2. Erstelle Collection '$COLLECTION_ID'..." -ForegroundColor Yellow
$createCollection = appwrite databases createCollection `
    --database-id $DATABASE_ID `
    --collection-id $COLLECTION_ID `
    --name $COLLECTION_NAME `
    --document-security false `
    2>&1

if ($LASTEXITCODE -eq 0) {
    Write-Host "   [OK] Collection '$COLLECTION_ID' erfolgreich erstellt!" -ForegroundColor Green
} else {
    if ($createCollection -like "*already exists*" -or $createCollection -like "*duplicate*") {
        Write-Host "   [INFO] Collection '$COLLECTION_ID' existiert bereits." -ForegroundColor Yellow
    } else {
        Write-Host "   [FEHLER] Fehler beim Erstellen der Collection:" -ForegroundColor Red
        Write-Host "   $createCollection" -ForegroundColor Red
        exit 1
    }
}

Write-Host ""

# 3. Erstelle Attribute
Write-Host "3. Erstelle Attribute..." -ForegroundColor Yellow

# 3.1 product_account_id (String, Required)
Write-Host "   3.1 product_account_id..." -ForegroundColor Gray
$attr1 = appwrite databases createStringAttribute `
    --database-id $DATABASE_ID `
    --collection-id $COLLECTION_ID `
    --key product_account_id `
    --required true `
    --size 255 `
    2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "      [OK] product_account_id erstellt" -ForegroundColor Green
} else {
    if ($attr1 -like "*already exists*" -or $attr1 -like "*duplicate*") {
        Write-Host "      [INFO] product_account_id existiert bereits" -ForegroundColor Yellow
    } else {
        Write-Host "      [WARNUNG] $attr1" -ForegroundColor Yellow
    }
}

# 3.2 product_platform (Enum, Required)
Write-Host "   3.2 product_platform..." -ForegroundColor Gray
$attr2 = appwrite databases createEnumAttribute `
    --database-id $DATABASE_ID `
    --collection-id $COLLECTION_ID `
    --key product_platform `
    --elements amazon ebay `
    --required true `
    2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "      [OK] product_platform erstellt" -ForegroundColor Green
} else {
    if ($attr2 -like "*already exists*" -or $attr2 -like "*duplicate*") {
        Write-Host "      [INFO] product_platform existiert bereits" -ForegroundColor Yellow
    } else {
        Write-Host "      [WARNUNG] $attr2" -ForegroundColor Yellow
    }
}

# 3.3 product_platform_market (String, Required)
Write-Host "   3.3 product_platform_market..." -ForegroundColor Gray
$attr3 = appwrite databases createStringAttribute `
    --database-id $DATABASE_ID `
    --collection-id $COLLECTION_ID `
    --key product_platform_market `
    --required true `
    --size 10 `
    2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "      [OK] product_platform_market erstellt" -ForegroundColor Green
} else {
    if ($attr3 -like "*already exists*" -or $attr3 -like "*duplicate*") {
        Write-Host "      [INFO] product_platform_market existiert bereits" -ForegroundColor Yellow
    } else {
        Write-Host "      [WARNUNG] $attr3" -ForegroundColor Yellow
    }
}

# 3.4 product_platform_product_id (String, Required)
Write-Host "   3.4 product_platform_product_id..." -ForegroundColor Gray
$attr4 = appwrite databases createStringAttribute `
    --database-id $DATABASE_ID `
    --collection-id $COLLECTION_ID `
    --key product_platform_product_id `
    --required true `
    --size 255 `
    2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "      [OK] product_platform_product_id erstellt" -ForegroundColor Green
} else {
    if ($attr4 -like "*already exists*" -or $attr4 -like "*duplicate*") {
        Write-Host "      [INFO] product_platform_product_id existiert bereits" -ForegroundColor Yellow
    } else {
        Write-Host "      [WARNUNG] $attr4" -ForegroundColor Yellow
    }
}

# 3.5 product_title (String, Optional)
Write-Host "   3.5 product_title..." -ForegroundColor Gray
$attr5 = appwrite databases createStringAttribute `
    --database-id $DATABASE_ID `
    --collection-id $COLLECTION_ID `
    --key product_title `
    --required false `
    --size 500 `
    2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "      [OK] product_title erstellt" -ForegroundColor Green
} else {
    if ($attr5 -like "*already exists*" -or $attr5 -like "*duplicate*") {
        Write-Host "      [INFO] product_title existiert bereits" -ForegroundColor Yellow
    } else {
        Write-Host "      [WARNUNG] $attr5" -ForegroundColor Yellow
    }
}

# 3.6 product_price (Float, Optional)
Write-Host "   3.6 product_price..." -ForegroundColor Gray
$attr6 = appwrite databases createFloatAttribute `
    --database-id $DATABASE_ID `
    --collection-id $COLLECTION_ID `
    --key product_price `
    --required false `
    2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "      [OK] product_price erstellt" -ForegroundColor Green
} else {
    if ($attr6 -like "*already exists*" -or $attr6 -like "*duplicate*") {
        Write-Host "      [INFO] product_price existiert bereits" -ForegroundColor Yellow
    } else {
        Write-Host "      [WARNUNG] $attr6" -ForegroundColor Yellow
    }
}

# 3.7 product_currency (String, Optional)
Write-Host "   3.7 product_currency..." -ForegroundColor Gray
$attr7 = appwrite databases createStringAttribute `
    --database-id $DATABASE_ID `
    --collection-id $COLLECTION_ID `
    --key product_currency `
    --required false `
    --size 10 `
    2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "      [OK] product_currency erstellt" -ForegroundColor Green
} else {
    if ($attr7 -like "*already exists*" -or $attr7 -like "*duplicate*") {
        Write-Host "      [INFO] product_currency existiert bereits" -ForegroundColor Yellow
    } else {
        Write-Host "      [WARNUNG] $attr7" -ForegroundColor Yellow
    }
}

# 3.8 product_url (String, Optional)
Write-Host "   3.8 product_url..." -ForegroundColor Gray
$attr8 = appwrite databases createStringAttribute `
    --database-id $DATABASE_ID `
    --collection-id $COLLECTION_ID `
    --key product_url `
    --required false `
    --size 1000 `
    2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "      [OK] product_url erstellt" -ForegroundColor Green
} else {
    if ($attr8 -like "*already exists*" -or $attr8 -like "*duplicate*") {
        Write-Host "      [INFO] product_url existiert bereits" -ForegroundColor Yellow
    } else {
        Write-Host "      [WARNUNG] $attr8" -ForegroundColor Yellow
    }
}

# 3.9 product_status (Enum, Optional)
Write-Host "   3.9 product_status..." -ForegroundColor Gray
$attr9 = appwrite databases createEnumAttribute `
    --database-id $DATABASE_ID `
    --collection-id $COLLECTION_ID `
    --key product_status `
    --elements active ended unknown `
    --required false `
    2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "      [OK] product_status erstellt" -ForegroundColor Green
} else {
    if ($attr9 -like "*already exists*" -or $attr9 -like "*duplicate*") {
        Write-Host "      [INFO] product_status existiert bereits" -ForegroundColor Yellow
    } else {
        Write-Host "      [WARNUNG] $attr9" -ForegroundColor Yellow
    }
}

# 3.10 product_category (String, Optional)
Write-Host "   3.10 product_category..." -ForegroundColor Gray
$attr10 = appwrite databases createStringAttribute `
    --database-id $DATABASE_ID `
    --collection-id $COLLECTION_ID `
    --key product_category `
    --required false `
    --size 255 `
    2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "      [OK] product_category erstellt" -ForegroundColor Green
} else {
    if ($attr10 -like "*already exists*" -or $attr10 -like "*duplicate*") {
        Write-Host "      [INFO] product_category existiert bereits" -ForegroundColor Yellow
    } else {
        Write-Host "      [WARNUNG] $attr10" -ForegroundColor Yellow
    }
}

# 3.11 product_condition (String, Optional)
Write-Host "   3.11 product_condition..." -ForegroundColor Gray
$attr11 = appwrite databases createStringAttribute `
    --database-id $DATABASE_ID `
    --collection-id $COLLECTION_ID `
    --key product_condition `
    --required false `
    --size 100 `
    2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "      [OK] product_condition erstellt" -ForegroundColor Green
} else {
    if ($attr11 -like "*already exists*" -or $attr11 -like "*duplicate*") {
        Write-Host "      [INFO] product_condition existiert bereits" -ForegroundColor Yellow
    } else {
        Write-Host "      [WARNUNG] $attr11" -ForegroundColor Yellow
    }
}

Write-Host ""
Write-Host "4. Erstelle Indexes..." -ForegroundColor Yellow

# 4.1 Index auf product_account_id
Write-Host "   4.1 Index auf product_account_id..." -ForegroundColor Gray
$idx1 = appwrite databases createIndex `
    --database-id $DATABASE_ID `
    --collection-id $COLLECTION_ID `
    --key idx_account_id `
    --type key `
    --attributes product_account_id `
    2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "      [OK] Index idx_account_id erstellt" -ForegroundColor Green
} else {
    if ($idx1 -like "*already exists*" -or $idx1 -like "*duplicate*") {
        Write-Host "      [INFO] Index idx_account_id existiert bereits" -ForegroundColor Yellow
    } else {
        Write-Host "      [WARNUNG] $idx1" -ForegroundColor Yellow
    }
}

# 4.2 Unique Index auf product_platform_product_id
Write-Host "   4.2 Unique Index auf product_platform_product_id..." -ForegroundColor Gray
$idx2 = appwrite databases createIndex `
    --database-id $DATABASE_ID `
    --collection-id $COLLECTION_ID `
    --key idx_platform_product_id `
    --type unique `
    --attributes product_platform_product_id `
    2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "      [OK] Index idx_platform_product_id erstellt" -ForegroundColor Green
} else {
    if ($idx2 -like "*already exists*" -or $idx2 -like "*duplicate*") {
        Write-Host "      [INFO] Index idx_platform_product_id existiert bereits" -ForegroundColor Yellow
    } else {
        Write-Host "      [WARNUNG] $idx2" -ForegroundColor Yellow
    }
}

# 4.3 Composite Index auf product_account_id + $createdAt
Write-Host "   4.3 Composite Index auf product_account_id + `$createdAt..." -ForegroundColor Gray
$idx3 = appwrite databases createIndex `
    --database-id $DATABASE_ID `
    --collection-id $COLLECTION_ID `
    --key idx_account_created `
    --type key `
    --attributes product_account_id `$createdAt `
    2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "      [OK] Index idx_account_created erstellt" -ForegroundColor Green
} else {
    if ($idx3 -like "*already exists*" -or $idx3 -like "*duplicate*") {
        Write-Host "      [INFO] Index idx_account_created existiert bereits" -ForegroundColor Yellow
    } else {
        Write-Host "      [WARNUNG] $idx3" -ForegroundColor Yellow
    }
}

Write-Host ""
Write-Host "=== Setup abgeschlossen ===" -ForegroundColor Green
Write-Host ""
Write-Host "Nächste Schritte:" -ForegroundColor Cyan
Write-Host "1. Prüfe die Berechtigungen in der Appwrite-Konsole" -ForegroundColor White
Write-Host "2. Stelle sicher, dass authentifizierte Benutzer Read/Write-Rechte haben" -ForegroundColor White
Write-Host "3. Teste die Collection mit einem Produkt-Scan" -ForegroundColor White
Write-Host ""
