
# fortune.ps1
# Simple CLI wrapper for FortuneBet backend

$Global:FortuneBetToken = $null
$Global:FortuneBetRefreshToken = $null
$Global:ApiBase = "http://127.0.0.1:8080/api/v1"
$Global:TenantSlug = "fortunebets"

function Invoke-FortuneAPI {
  param(
    [string]$Method,
    [string]$Path,
    [object]$Body = $null,
    [switch]$Tenant
  )

  $url = if ($Tenant) {
    "$Global:ApiBase/tenant/$Global:TenantSlug/$Path"
  } else {
    "$Global:ApiBase/$Path"
  }

  $headers = @{ "Content-Type" = "application/json" }
  if ($Global:FortuneBetToken) {
    $headers["Authorization"] = "Bearer $($Global:FortuneBetToken)"
  }

  $bodyJson = if ($Body) {
    ($Body | ConvertTo-Json -Depth 5 -Compress) 
  } else {
    $null 
  }
  Write-Host "➡️ $Method $url"
  Write-Host "Headers:" ($headers | ConvertTo-Json -Compress)
  try {
    if ($Method -eq "GET" -or $Method -eq "HEAD") {
      Invoke-RestMethod -Method $Method -Uri $url -Headers $headers -Verbose
    } elseif ($BodyJson) {
      Invoke-RestMethod -Method $Method -Uri $url -Headers $headers -Body $bodyJson -Verbose
    } else {
      Invoke-RestMethod -Method $Method -Uri $url -Headers $headers -Verbose
    }
  } catch {
    Write-Error "Request failed: $($_.Exception.Message)"
    if ($_.ErrorDetails) {
      Write-Host $_.ErrorDetails 
    }
  }
}

function fortune {
  param(
    [Parameter(Position = 0)]
    [string]$Command,

    [Parameter(Position = 1)]
    [string]$Method,

    [Parameter(Position = 2)]
    [string]$Path,

    [Parameter(Position = 3)]
    [object]$Body,

    [switch]$Tenant,

    [string]$Email,
    [string]$Password
  )

  switch ($Command) {
    "login" {
      if (-not $Email -or -not $Password) {
        Write-Error "Usage: fortune login -Email <email> -Password <password>"
        return
      }

      $payload = @{
        email    = $Email
        password = $Password
      }

      $resp = Invoke-FortuneAPI -Method "POST" -Path "auth/customer-login" -Tenant -Body $payload
      echo $resp.data
      if ($resp -and $resp.data.access_token) {
        $Global:FortuneBetToken = $resp.data.access_token
        $Global:FortuneBetRefreshToken = $resp.data.refresh_token
        Write-Host "✅ Logged in."
      } else {
        Write-Error "❌ Login failed."
      }
    }
    "login-super" {
      if (-not $Email -or -not $Password) {
        Write-Error "Usage: fortune login-super -Email <email> -Password <password>"
        return
      }

      $payload = @{
        email    = $Email
        password = $Password
      }

      $resp = Invoke-FortuneAPI -Method "POST" -Path "auth/super-login" -Body $payload
      echo $resp.data
      if ($resp -and $resp.data.access_token) {
        $Global:FortuneBetToken = $resp.data.access_token
        $Global:FortuneBetRefreshToken = $resp.data.refresh_token
        Write-Host "✅ Logged in."
      } else {
        Write-Error "❌ Login failed."
      }
    }
    "login-admin" {
      if (-not $Email -or -not $Password) {
        Write-Error "Usage: fortune login-admin -Email <email> -Password <password>"
        return
      }

      $payload = @{
        email    = $Email
        password = $Password
      }

      $resp = Invoke-FortuneAPI -Method "POST" -Path "auth/admin-login" -Tenant -Body $payload
      echo $resp.data
      if ($resp -and $resp.data.access_token) {
        $Global:FortuneBetToken = $resp.data.access_token
        $Global:FortuneBetRefreshToken = $resp.data.refresh_token
        Write-Host "✅ Logged in."
      } else {
        Write-Error "❌ Login failed."
      }
    }
    "refresh" {
      if (-not $Global:FortuneBetRefreshToken) {
        Write-Error "No refresh token stored."
        return
      }

      $payload = @{
        refresh_token = $Global:FortuneBetRefreshToken  
        access_token = $Global:FortuneBetToken
      }
      $resp = Invoke-FortuneAPI -Method "POST" -Path "auth/refresh" -Body $payload
      if ($resp -and $resp.data.access_token) {
        $Global:FortuneBetToken = $resp.data.access_token
        Write-Host "🔄 Token refreshed."
      } else {
        Write-Error "❌ Refresh failed."
      }
    }
    "call" {
      if (-not $Method -or -not $Path) {
        Write-Error "Usage: fortune call <METHOD> <PATH> [-Tenant] [-Body @{...}]"
        return
      }

      $resp = Invoke-FortuneAPI -Method $Method -Path $Path -Tenant:$Tenant -Body $Body
      if ($resp) {
        $resp | ConvertTo-Json -Depth 10
      }
    }
    default {
      Write-Host "Fortune CLI"
      Write-Host ""
      Write-Host "Commands:"
      Write-Host "  fortune login -Email <email> -Password <password>"
      Write-Host "  fortune refresh"
      Write-Host "  fortune call <METHOD> <PATH> [-Tenant] [-Body @{...}]"
    }
  }
}
