# Appwrite schema script für account_metrics Collection
# Erstellt die account_metrics Collection mit allen Attributen und Indexes
#
# Prereqs:
#   appwrite login
#   appwrite init project
#
# Run:
#   pwsh .\create-account-metrics-collection.ps1 -DatabaseId "YOUR_DATABASE_ID"

param(
  [Parameter(Mandatory = $true)]
  [string]$DatabaseId
)

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"

# ---------------- CONFIG ----------------
$T_ACCOUNT_METRICS = "account_metrics"

# Minimal offene Defaults
$PERMS_ANY_CRUD = @('create(any)','read(any)','update(any)','delete(any)')

# ---------------- HELPERS ----------------
function Try-Cmd {
  param(
    [Parameter(Mandatory = $true)]
    [string[]]$Args
  )

  $cmd = "appwrite " + ($Args -join " ")
  Write-Host ("+ " + $cmd)

  try {
    & appwrite @Args | Out-Host
  } catch {
    Write-Host ("  (ignored error) " + $_.Exception.Message)
  }
}

function Create-Table {
  param(
    [Parameter(Mandatory = $true)][string]$TableId,
    [Parameter(Mandatory = $true)][string]$Name
  )

  $argsList = @(
    "tables-db","create-table",
    "--database-id",$DatabaseId,
    "--table-id",$TableId,
    "--name",$Name,
    "--row-security","false"
  )
  
  Try-Cmd $argsList
}

# ---------------- CREATE TABLE ----------------
Create-Table -TableId $T_ACCOUNT_METRICS -Name "account_metrics"

# ---------------- ACCOUNT_METRICS COLUMNS ----------------
# Required attributes
Try-Cmd @("tables-db","create-string-column","--database-id",$DatabaseId,"--table-id",$T_ACCOUNT_METRICS,"--key","account_metrics_account_id","--size","64","--required","true","--array","false")
Try-Cmd @("tables-db","create-string-column","--database-id",$DatabaseId,"--table-id",$T_ACCOUNT_METRICS,"--key","account_metrics_date","--size","10","--required","true","--array","false")
Try-Cmd @("tables-db","create-string-column","--database-id",$DatabaseId,"--table-id",$T_ACCOUNT_METRICS,"--key","account_metrics_month","--size","7","--required","true","--array","false")

# Refresh layer attributes
Try-Cmd @("tables-db","create-boolean-column","--database-id",$DatabaseId,"--table-id",$T_ACCOUNT_METRICS,"--key","account_metrics_refreshed","--required","true","--array","false")
Try-Cmd @("tables-db","create-enum-column","--database-id",$DatabaseId,"--table-id",$T_ACCOUNT_METRICS,"--key","account_metrics_refresh_status","--elements",'["success","failed"]',"--required","true","--array","false")
Try-Cmd @("tables-db","create-datetime-column","--database-id",$DatabaseId,"--table-id",$T_ACCOUNT_METRICS,"--key","account_metrics_refreshed_at","--required","false","--array","false")

# Sales layer attributes
Try-Cmd @("tables-db","create-integer-column","--database-id",$DatabaseId,"--table-id",$T_ACCOUNT_METRICS,"--key","account_metrics_sales_count","--required","false","--array","false")
Try-Cmd @("tables-db","create-string-column","--database-id",$DatabaseId,"--table-id",$T_ACCOUNT_METRICS,"--key","account_metrics_sales_bucket","--size","10","--required","false","--array","false")

# ---------------- ACCOUNT_METRICS INDEXES ----------------
# Unique index: ensures one doc per account per day
Try-Cmd @("tables-db","create-index","--database-id",$DatabaseId,"--table-id",$T_ACCOUNT_METRICS,"--key","uniq_account_date","--type","unique","--columns","account_metrics_account_id","account_metrics_date")

# Index for month queries
Try-Cmd @("tables-db","create-index","--database-id",$DatabaseId,"--table-id",$T_ACCOUNT_METRICS,"--key","idx_account_month","--type","key","--columns","account_metrics_account_id","account_metrics_month")

Write-Host ""
Write-Host "account_metrics Collection erstellt!"
Write-Host ""
