-- Apply this patch after you have created the base schema (users/payments/payment_settings).

-- 1) TonConnect payment metadata
CREATE TABLE IF NOT EXISTS tonconnect_payments (
    uuid VARCHAR(64) PRIMARY KEY,
    ton_amount NUMERIC(20, 9) NOT NULL,
    comment VARCHAR(128) NOT NULL,
    tx_hash VARCHAR(128),
    created_at TIMESTAMP DEFAULT now(),
    paid_at TIMESTAMP
);

-- 2) Add/Update payment settings
-- CryptoBot (Crypto Pay) = fiat RUB invoices
INSERT INTO payment_settings (system, min_amount, currency, exchange_rate)
VALUES ('cryptobot', 50, 'RUB', NULL)
ON CONFLICT (system) DO UPDATE
SET min_amount = EXCLUDED.min_amount,
    currency = EXCLUDED.currency,
    exchange_rate = EXCLUDED.exchange_rate,
    updated_at = now();

-- TonConnect deposit is paid in TON, balance is credited in RUB using exchange_rate (RUB per 1 TON)
-- IMPORTANT: set exchange_rate yourself (example value below).
INSERT INTO payment_settings (system, min_amount, currency, exchange_rate)
VALUES ('tonconnect', 50, 'RUB', 500)
ON CONFLICT (system) DO UPDATE
SET min_amount = EXCLUDED.min_amount,
    currency = EXCLUDED.currency,
    exchange_rate = EXCLUDED.exchange_rate,
    updated_at = now();
