\q
--
-- PostgreSQL database dump
--

-- Dumped from database version 15.13
-- Dumped by pg_dump version 15.13

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: telegram_stars; Type: SCHEMA; Schema: -; Owner: postgres
--

CREATE SCHEMA telegram_stars;


ALTER SCHEMA telegram_stars OWNER TO postgres;

--
-- Name: convert_usd_to_stars(numeric); Type: FUNCTION; Schema: telegram_stars; Owner: postgres
--

CREATE FUNCTION telegram_stars.convert_usd_to_stars(amount_usd numeric) RETURNS integer
    LANGUAGE plpgsql
    AS $$
DECLARE
    stars_amount INTEGER;
    exchange_rate NUMERIC;
BEGIN
    exchange_rate := telegram_stars.get_exchange_rate();
    stars_amount := GREATEST(CEIL(amount_usd * exchange_rate), 1);
    
    RETURN stars_amount;
END $$;


ALTER FUNCTION telegram_stars.convert_usd_to_stars(amount_usd numeric) OWNER TO postgres;

--
-- Name: create_invoice(numeric, text, text); Type: FUNCTION; Schema: telegram_stars; Owner: postgres
--

CREATE FUNCTION telegram_stars.create_invoice(amount_usd numeric, order_id text, description text DEFAULT 'Proxy traffic purchase'::text) RETURNS json
    LANGUAGE plpgsql
    AS $$
DECLARE
    stars_amount INTEGER;
    exchange_rate NUMERIC;
    invoice_data JSONB;
BEGIN
    -- Get current USD to Stars exchange rate (1 USD = ~7.5 Stars as of 2024)
    -- This should be configurable via system settings
    exchange_rate := COALESCE(
        (SELECT value::NUMERIC FROM legacy.system WHERE key = 'usd_to_stars_rate'),
        7.5
    );
    
    -- Convert USD to Stars (minimum 1 star)
    stars_amount := GREATEST(CEIL(amount_usd * exchange_rate), 1);
    
    -- Create invoice data for Telegram Bot API
    invoice_data := jsonb_build_object(
        'order_id', order_id,
        'amount_usd', amount_usd,
        'stars_amount', stars_amount,
        'exchange_rate', exchange_rate,
        'description', description,
        'currency', 'XTR',
        'created_at', NOW()
    );
    
    RETURN invoice_data;
END $$;


ALTER FUNCTION telegram_stars.create_invoice(amount_usd numeric, order_id text, description text) OWNER TO postgres;

--
-- Name: get_exchange_rate(); Type: FUNCTION; Schema: telegram_stars; Owner: postgres
--

CREATE FUNCTION telegram_stars.get_exchange_rate() RETURNS numeric
    LANGUAGE plpgsql
    AS $$
DECLARE
    rate NUMERIC;
BEGIN
    SELECT COALESCE(
        (SELECT value::NUMERIC FROM legacy.system WHERE key = 'usd_to_stars_rate'),
        7.5
    ) INTO rate;
    
    RETURN rate;
END $$;


ALTER FUNCTION telegram_stars.get_exchange_rate() OWNER TO postgres;

--
-- Name: process_payment(jsonb); Type: FUNCTION; Schema: telegram_stars; Owner: postgres
--

CREATE FUNCTION telegram_stars.process_payment(payment_data jsonb) RETURNS boolean
    LANGUAGE plpgsql
    AS $$
DECLARE
    order_id TEXT;
    stars_amount INTEGER;
    user_id TEXT;
    amount_gb INTEGER;
    pool_type TEXT;
    promocode TEXT;
BEGIN
    -- Extract payment data
    order_id := payment_data->>'order_id';
    stars_amount := (payment_data->>'stars_amount')::INTEGER;
    user_id := payment_data->>'user_id';
    amount_gb := (payment_data->>'amount_gb')::INTEGER;
    pool_type := payment_data->>'pool_type';
    promocode := payment_data->>'promocode';
    
    -- Validate required fields
    IF order_id IS NULL OR user_id IS NULL OR amount_gb IS NULL OR pool_type IS NULL THEN
        RAISE EXCEPTION 'Missing required payment data';
    END IF;
    
    -- Log the payment
    INSERT INTO legacy.payment (
        user_id,
        paid,
        order_id,
        amount_gb,
        service,
        source,
        date
    ) VALUES (
        user_id,
        stars_amount / 7.5, -- Convert back to USD for legacy compatibility
        order_id,
        amount_gb,
        'telegram_stars',
        'bot',
        to_char(NOW(), 'YYYY-MM-DD HH24:MI:SS')
    );
    
    -- Add traffic to user's balance
    -- This should call the same function that other payment methods use
    PERFORM vgate.add_traffic_to_user(
        user_id::UUID,
        amount_gb,
        pool_type::common.dataimpulsepool,
        promocode
    );
    
    RETURN TRUE;
EXCEPTION
    WHEN OTHERS THEN
        RAISE EXCEPTION 'Failed to process Telegram Stars payment: %', SQLERRM;
END $$;


ALTER FUNCTION telegram_stars.process_payment(payment_data jsonb) OWNER TO postgres;

--
-- Name: SCHEMA telegram_stars; Type: ACL; Schema: -; Owner: postgres
--

GRANT ALL ON SCHEMA telegram_stars TO vgate_provider;


--
-- PostgreSQL database dump complete
--

