# CryptoPay Tests
function TestCryptoPayDeposit {
  $request = @{
    amount=200
    currency="ETB"
    crypto_currency="USDT"
  }
  fortune call POST "cryptopay/deposit" -Body $request -Tenant
}
function TestCryptoPayWithdraw {
  param(
    [string]$address
  ) 
  $request = @{
    address=$address
    amount=200
    currency="ETB"
    crypto_currency="USDT"
  }
  fortune call POST "cryptopay/withdrawal" -Body $request -Tenant
}
# Chapa Tests
function TestChapaDeposit {
  $request = @{
    amount=200
  }
  fortune call POST "chapa/payments/deposit" -Body $request
}

function TestChapaBanks {
  fortune call GET "chapa/banks" 
}

function TestChapaWithdraw {
  $request = @{
    account_name="Samuel Tariku"
    account_number="0946685511"
    amount="200"
    reference="1"
    bank_code=128 #CBEBirr
  }
  fortune call POST "chapa/payments/withdraw" -Body $request
}

# Arifpay Tests
function TestArifPayDeposit {
  $request = @{
    amount=200
    customerEmail="samueltarikufantaye@gmail.com"
    customerPhone="0946685511"
  }
  fortune call POST "arifpay/checkout" -Body $request
}

# function TestARIFCheckout {
#   $request = @{
#     cancelUrl="https://example1.com"
#     phone="251922655097"
#     email="Ex@gmail.net"
#     nonce="251assaddsasad93554asdasd8208sawas"
#     errorUrl="http://error.com"
#     notifyUrl="https://664db983ede9a2b5565497ee.mockapi.io/user"
#     successUrl="http://example.com"
#     paymentMethods=
#     expireDate=
#     items=
#     beneficiaries=
#     lang=
#
#   }
# }

#
#  Testing Direct Deposit 
#
function CreateDirectDeposit {
  $deposit = @{
    bank_name="cbe"
    sender_account_number="123456789"
    sender_account_holder="Samuel Tariku"
    receiver_account_number="123456789"
    receiver_account_holder="Fortune Admin"
    amount=1000.0
    reference_number="123456"
  } 
  fortune call POST "direct-deposits" -Tenant -Body $deposit
}
function GetAllDirectDeposits {
  fortune call GET "direct-deposits?status=PENDING" | less
}

function ApproveDirectDeposit {
  param(
    [Int64]$id
  ) 
  fortune call POST "direct-deposits/$id/approve" | less
}


function RejectDirectDeposit {
  param(
    [Int64]$id
  ) 
  
  $reject = @{
    reason="Incorrect account number"
  }
  fortune call POST "direct-deposits/$id/reject" -Body $reject | less
}

#
# Testing Super Admin Bank
#

function CreateBank {
  $bank = @{
    swift="CBETTA"
    name="Commerical Bank Of Ethiopia"
    acct_length=10
    country_code="et"
  } 
  fortune call POST "bank" -Body $bank
}

function GetAllBanks {
  fortune call GET "bank" | less
}
function GetBankByID {
  param(
    [Int64]$id
  ) 
  fortune call GET "banks/$id" | less
}
function DeleteBank {
  param(
    [Int64]$id
  ) 
  fortune call DELETE "banks/$id" | less
}
#
#Testing Direct Deposit Bank
#

function CreateDDBank {
  param(
    [Int64]$id
  ) 
  $bank = @{
    company_id=1
    bank_id=$id
    interval=1
    is_cycle=$true
  } 
  fortune call POST "direct-deposit-bank" -Body $bank
}

function GetAllDDBanks {
  fortune call GET "direct-deposit-bank" | less
}
function GetDDBankByID {
  param(
    [Int64]$id
  ) 
  fortune call GET "direct-deposit-bank/$id" | less
}
function ManuallyCycleBank {
  param(
    [Int64]$id
  ) 
  fortune call POST "direct-deposit-bank/$id/cycle" | less
}
function DeleteDDBank {
  param(
    [Int64]$id
  ) 
  fortune call DELETE "direct-deposit-bank/$id" | less
}

# Testing Direct Deposit Account
#
function CreateDDAccount {
  param(
    [Int64]$id
  ) 
  $account = @{
    dd_bank_id=$id
    account_number="1234567"
    account_holder="Fortune Admin"
  } 
  fortune call POST "direct-deposit-account" -Body $account
}
function GetDDAccountForBank {
  param(
    [Int64]$id
  ) 
  fortune call GET "direct-deposit-bank/$id/accounts" | less
}


function GetVirtualGameProviders {
  fortune call GET "virtual-game/orchestrator/providers"
}
function GetVirtualGameList {
  fortune call GET "virtual-game/orchestrator/games?providerID=veliplay"
}

function StartVeliGameDemo {
  param(
    [Int64]$gameID,
    [string]$providerID
  ) 

  $gameData = @{
    providerId=$providerID
    gameId=$gameID
    language="en"
    deviceType= "DESKTOP"
    brandId="fortune_bets"
  }

  fortune call POST "veli/start-demo-game" -Body $gameData
}


function GetLeagues {
  fortune call GET "leagues" -Tenant 
}

function GetEvents {
  fortune call GET "upcoming-events" -Tenant
}
function GetOdds {
  param(
    [Int64]$eventID
  ) 
  fortune call GET "odds/upcoming/${eventID}" -Tenant
}

function GetOdds {
  param(
    [Int64]$eventID
  ) 
  fortune call GET "odds/upcoming/${eventID}" -Tenant
}

function Select-Event {
  param ($Events)

  $selection = $Events |
    ForEach-Object {
      "$($_.id),$($_.match_name),$($_.league_name),$($_.start_time)"
    } |
    fzf --delimiter=',' --with-nth=2,3,4

  if ($selection) {
    $selectedId = ($selection -split ",")[0]
    return $Events | Where-Object { $_.id -eq [int]$selectedId }
  }
}
# @{id=29169722; event_id=1732385; market_type=winning_margin; market_name=Winning Margin; market_category=others; market_id=56; number_of_outcomes=10; raw_odds=System.Object[]; fetched_at=12/9/2025 04:15:38; expires_at=12/9/2025 05:15:38; is_active=True}
function Select-Odd {
  param ($Odds)
  $selection = $Odds | ForEach-Object {
    "$($_.id),$($_.market_name)"
  } | fzf --delimiter=',' --with-nth=2

  if ($selection) {
    $selectedId = ($selection -split ",")[0]
    return $Odds | Where-Object { $_.id -eq [int]$selectedId }
  }
}

#@{id=4967715; name=1; odds=1.500} @{id=4967716; name=Draw; odds=4.000} @{id=4967717; name=2; odds=5.500}
#@{id=4967741; odds=1.925; header=1; handicap=-1.0} @{id=4967746; odds=1.875; header=2; handicap=+1.0}
function Select-RawOdd {
  param ($Odds)
  $selection = $Odds | ForEach-Object {
    "$($_.id),$($_.name),$($_.header),$($_.handicap),"
  } | fzf --delimiter=',' --with-nth=2,3,4

  if ($selection) {
    $selectedId = ($selection -split ",")[0]
    return $Odds | Where-Object { $_.id -eq [int]$selectedId }
  }
}

function EventOddPipe {
  $events = GetEvents | ConvertFrom-Json
  $selectedEvent = Select-Event $events.data
  Write-Host $selectedEvent.id
  $odds = GetOdds $selectedEvent.id | ConvertFrom-Json
  $selectedOdd = Select-Odd $odds.data
  #Write-Host $selectedOdd.raw_odds
  $selectedRawOdd = Select-RawOdd $selectedOdd.raw_odds
  Write-Host $selectedRawOdd
  
  return @{
    selectedEvent=$selectedEvent
    selectedOdd=$selectedOdd
    selectedRawOdd=$selectedRawOdd
  }
}
# Create Bet
function CreateBet {

  $eventOdd = EventOddPipe

  Write-Host $eventOdd

  $req = @{
    outcomes = @(
      @{
        event_id=[int64]$eventOdd.selectedEvent.id
        odd_id=[int64]$eventOdd.selectedRawOdd.id
        market_id=[int64]$eventOdd.selectedOdd.market_id
      }
    )
    amount=30.0
  }
  fortune call POST "sport/bet" -Tenant -Body $req
}
# Login Tests
function LoginClient{
  fortune login -Email john.doe@example.com -Password password@123
}
function LoginAdmin{
  fortune login-admin -Email test.admin@gmail.com -Password NLd3m6taW71H
}
function LoginSuper{
  fortune login-super -Email cybersamt@gmail.com -Password H6xW6pe1w8ys
}

# Register Tasks
function Register {
  param(
    [string]$otp
  ) 
  $req = @{
    first_name="Samuel"
    last_name="Tariku"
    phone_number="+251900000002"
    password="password@123"
    otp=$otp
    referral_code=""
  }
  fortune call POST "user/register" -Tenant $req
}

function SendRegisterOTP {
  $req = @{
    phone_number="+251900000002"
  }
  fortune call POST "user/sendRegisterCode" -Tenant $req
}

function Main {
  $scriptPath = $PSCommandPath

  $functions = Get-ChildItem Function: |
    Where-Object {
      $_.ScriptBlock.File -eq $scriptPath -and
      $_.Name -ne "Main"
    } |
    Select-Object -ExpandProperty Name |
    Sort-Object

  $fn = $functions | fzf --prompt="Select action > "
  if (-not $fn) {
    return 
  }

  $command = Get-Command $fn
  $params  = @{}

  foreach ($p in $command.Parameters.Values) {
    $params[$p.Name] = Read-Host "Enter $($p.Name)"
  }

  & $fn @params
}

Main
