-- Sandbox currency operator profile template.
-- Review limits and callback URLs before running against Supabase/Postgres.
-- Do not store real shared secrets here.
-- Put operator secrets in deployment env vars such as:
--   OPERATOR_SECRET_SANDBOX_USD
--   OPERATOR_SECRET_SANDBOX_UBTC
--   OPERATOR_SECRET_SANDBOX_ETH
--
-- V1 uses one active currency per operator profile. If a casino wants a
-- player to switch from USD to uBTC, the casino should create a new launch
-- token for the matching operator id and send the currency-switch message
-- documented in docs/API.md.

WITH profiles (
  operator_id,
  display_name,
  currency,
  min_bet,
  max_bet,
  max_payout,
  stake_step
) AS (
  VALUES
    ('sandbox-usd', 'Rouge Reels Sandbox USD', 'USD', 0.10, 500.00, 50000.00, 0.10),
    ('sandbox-eur', 'Rouge Reels Sandbox EUR', 'EUR', 0.10, 500.00, 50000.00, 0.10),
    ('sandbox-gbp', 'Rouge Reels Sandbox GBP', 'GBP', 0.10, 500.00, 50000.00, 0.10),
    ('sandbox-usdt', 'Rouge Reels Sandbox USDT', 'USDT', 0.10, 500.00, 50000.00, 0.10),
    ('sandbox-usdc', 'Rouge Reels Sandbox USDC', 'USDC', 0.10, 500.00, 50000.00, 0.10),
    ('sandbox-btc', 'Rouge Reels Sandbox BTC', 'BTC', 0.00001000, 0.05000000, 5.00000000, 0.00000100),
    ('sandbox-ubtc', 'Rouge Reels Sandbox uBTC', 'uBTC', 1.00, 50000.00, 5000000.00, 0.01),
    ('sandbox-eth', 'Rouge Reels Sandbox ETH', 'ETH', 0.000100, 1.000000, 100.000000, 0.000001),
    ('sandbox-ltc', 'Rouge Reels Sandbox LTC', 'LTC', 0.00100000, 10.00000000, 1000.00000000, 0.00000100),
    ('sandbox-doge', 'Rouge Reels Sandbox DOGE', 'DOGE', 1.00000000, 50000.00000000, 5000000.00000000, 0.00000100),
    ('sandbox-bnb', 'Rouge Reels Sandbox BNB', 'BNB', 0.00010000, 5.00000000, 500.00000000, 0.00000100),
    ('sandbox-sol', 'Rouge Reels Sandbox SOL', 'SOL', 0.00100000, 50.00000000, 5000.00000000, 0.00000100),
    ('sandbox-xrp', 'Rouge Reels Sandbox XRP', 'XRP', 0.100000, 50000.000000, 5000000.000000, 0.000001),
    ('sandbox-ada', 'Rouge Reels Sandbox ADA', 'ADA', 0.100000, 50000.000000, 5000000.000000, 0.000001),
    ('sandbox-trx', 'Rouge Reels Sandbox TRX', 'TRX', 1.000000, 100000.000000, 10000000.000000, 0.000001),
    ('sandbox-xlm', 'Rouge Reels Sandbox XLM', 'XLM', 0.1000000, 50000.0000000, 5000000.0000000, 0.0000001),
    ('sandbox-zec', 'Rouge Reels Sandbox ZEC', 'ZEC', 0.00010000, 5.00000000, 500.00000000, 0.00000100)
)
INSERT INTO public.operators (
  id,
  name,
  status,
  callback_base_url,
  currency,
  theme
)
SELECT
  operator_id,
  display_name,
  'active',
  'https://casino-sandbox.example.com',
  currency,
  jsonb_build_object(
    'brandName', display_name,
    'logoText', 'RR',
    'primary', '#16a86f',
    'accent', '#ffd166',
    'background', '#0d1117'
  )
FROM profiles
ON CONFLICT (id) DO UPDATE SET
  name = EXCLUDED.name,
  status = EXCLUDED.status,
  callback_base_url = EXCLUDED.callback_base_url,
  currency = EXCLUDED.currency,
  theme = EXCLUDED.theme,
  updated_at = now();

WITH profiles (
  operator_id,
  currency,
  min_bet,
  max_bet,
  max_payout,
  stake_step
) AS (
  VALUES
    ('sandbox-usd', 'USD', 0.10, 500.00, 50000.00, 0.10),
    ('sandbox-eur', 'EUR', 0.10, 500.00, 50000.00, 0.10),
    ('sandbox-gbp', 'GBP', 0.10, 500.00, 50000.00, 0.10),
    ('sandbox-usdt', 'USDT', 0.10, 500.00, 50000.00, 0.10),
    ('sandbox-usdc', 'USDC', 0.10, 500.00, 50000.00, 0.10),
    ('sandbox-btc', 'BTC', 0.00001000, 0.05000000, 5.00000000, 0.00000100),
    ('sandbox-ubtc', 'uBTC', 1.00, 50000.00, 5000000.00, 0.01),
    ('sandbox-eth', 'ETH', 0.000100, 1.000000, 100.000000, 0.000001),
    ('sandbox-ltc', 'LTC', 0.00100000, 10.00000000, 1000.00000000, 0.00000100),
    ('sandbox-doge', 'DOGE', 1.00000000, 50000.00000000, 5000000.00000000, 0.00000100),
    ('sandbox-bnb', 'BNB', 0.00010000, 5.00000000, 500.00000000, 0.00000100),
    ('sandbox-sol', 'SOL', 0.00100000, 50.00000000, 5000.00000000, 0.00000100),
    ('sandbox-xrp', 'XRP', 0.100000, 50000.000000, 5000000.000000, 0.000001),
    ('sandbox-ada', 'ADA', 0.100000, 50000.000000, 5000000.000000, 0.000001),
    ('sandbox-trx', 'TRX', 1.000000, 100000.000000, 10000000.000000, 0.000001),
    ('sandbox-xlm', 'XLM', 0.1000000, 50000.0000000, 5000000.0000000, 0.0000001),
    ('sandbox-zec', 'ZEC', 0.00010000, 5.00000000, 500.00000000, 0.00000100)
),
game_rows AS (
  SELECT
    operator_id,
    'dice' AS game_type,
    true AS enabled,
    min_bet,
    max_bet,
    max_payout,
    0.01 AS house_edge,
    jsonb_build_object(
      'directions', jsonb_build_array('over', 'under'),
      'targetMin', 1,
      'targetMax', 99,
      'stakeStep', stake_step,
      'currency', currency
    ) AS profile
  FROM profiles
  UNION ALL
  SELECT
    operator_id,
    'limbo',
    true,
    min_bet,
    max_bet / 2,
    max_payout,
    0.01,
    jsonb_build_object(
      'targetMultiplierMin', 1.01,
      'targetMultiplierMax', 1000,
      'stakeStep', stake_step,
      'currency', currency
    )
  FROM profiles
  UNION ALL
  SELECT
    operator_id,
    'plinko',
    true,
    min_bet,
    max_bet / 5,
    max_payout,
    NULL,
    jsonb_build_object(
      'rows', 12,
      'rowOptions', jsonb_build_array(8, 9, 10, 11, 12, 13, 14, 15, 16),
      'risk', 'medium',
      'riskOptions', jsonb_build_array('low', 'medium', 'high'),
      'stakeStep', stake_step,
      'currency', currency
    )
  FROM profiles
)
INSERT INTO public.operator_game_configs (
  operator_id,
  game_type,
  enabled,
  min_bet,
  max_bet,
  max_payout,
  house_edge,
  profile
)
SELECT
  operator_id,
  game_type,
  enabled,
  min_bet,
  max_bet,
  max_payout,
  house_edge,
  profile
FROM game_rows
ON CONFLICT (operator_id, game_type) DO UPDATE SET
  enabled = EXCLUDED.enabled,
  min_bet = EXCLUDED.min_bet,
  max_bet = EXCLUDED.max_bet,
  max_payout = EXCLUDED.max_payout,
  house_edge = EXCLUDED.house_edge,
  profile = EXCLUDED.profile,
  updated_at = now();
