<?php
/**
 * EasyStream Monetization System
 * Memberships, Super Chat, Ads, Revenue Sharing
 * Stripe & PayPal Integration
 * Version: 1.0
 */

defined('_ISVALID') or header('Location: /error');

class VMonetization {
    private static $db;
    private static $stripe_secret_key = null;
    private static $stripe_publishable_key = null;

    public static function init() {
        self::$db = VDatabase::getInstance();

        // Load Stripe configuration
        $config = VGenerate::getConfig('stripe_config');
        if ($config) {
            self::$stripe_secret_key = $config['secret_key'] ?? null;
            self::$stripe_publishable_key = $config['publishable_key'] ?? null;
        }
    }

    /**
     * Create membership tier
     * @param int $usr_id Channel owner ID
     * @param string $name Tier name
     * @param float $price_monthly Monthly price
     * @param array $perks List of perks
     * @return int Tier ID
     */
    public static function createMembershipTier($usr_id, $name, $price_monthly, $perks = []) {
        self::init();

        $usr_id = (int)$usr_id;
        $name_safe = VDatabase::escape($name);
        $price = (float)$price_monthly;
        $perks_json = VDatabase::escape(json_encode($perks));

        $sql = "INSERT INTO db_membership_tiers
                (usr_id, name, price_monthly, currency, perks, is_active, created_at)
                VALUES ($usr_id, '$name_safe', $price, 'USD', '$perks_json', 1, NOW())";

        if (self::$db->execute($sql)) {
            return self::$db->insert_id();
        }

        return false;
    }

    /**
     * Subscribe to membership
     * @param int $tier_id Tier ID
     * @param int $subscriber_id Subscriber user ID
     * @param string $payment_method Payment method
     * @return int Membership ID
     */
    public static function subscribeMembership($tier_id, $subscriber_id, $payment_method = 'stripe') {
        self::init();

        $tier_id = (int)$tier_id;
        $subscriber_id = (int)$subscriber_id;

        // Get tier details
        $sql = "SELECT * FROM db_membership_tiers WHERE tier_id = $tier_id AND is_active = 1";
        $result = self::$db->execute($sql);

        if (!$result || $result->RecordCount() == 0) {
            return false;
        }

        $tier = $result->FetchRow();
        $channel_owner_id = (int)$tier['usr_id'];
        $price = (float)$tier['price_monthly'];

        // Create Stripe subscription (if using Stripe)
        $stripe_sub_id = null;
        if ($payment_method == 'stripe' && self::$stripe_secret_key) {
            $stripe_sub_id = self::createStripeSubscription($subscriber_id, $price);
        }

        $stripe_sub_safe = $stripe_sub_id ? "'" . VDatabase::escape($stripe_sub_id) . "'" : 'NULL';
        $payment_safe = VDatabase::escape($payment_method);

        $sql = "INSERT INTO db_memberships
                (tier_id, subscriber_id, channel_owner_id, status, started_at, expires_at, payment_method, stripe_subscription_id)
                VALUES ($tier_id, $subscriber_id, $channel_owner_id, 'active', NOW(), DATE_ADD(NOW(), INTERVAL 1 MONTH), '$payment_safe', $stripe_sub_safe)";

        if (self::$db->execute($sql)) {
            // Record transaction
            self::recordTransaction($subscriber_id, 'membership', $price, "Membership: {$tier['name']}");
            return self::$db->insert_id();
        }

        return false;
    }

    /**
     * Create Stripe subscription (simplified)
     */
    private static function createStripeSubscription($usr_id, $amount) {
        // Placeholder for Stripe API integration
        // In production, use Stripe PHP SDK
        return 'sub_' . bin2hex(random_bytes(16));
    }

    /**
     * Send Super Chat
     * @param int $usr_id Sender user ID
     * @param int $recipient_id Recipient user ID
     * @param string $file_key Associated file (optional)
     * @param float $amount Amount in USD
     * @param string $message Message
     * @return int Super chat ID
     */
    public static function sendSuperChat($usr_id, $recipient_id, $file_key, $amount, $message = '') {
        self::init();

        $usr_id = (int)$usr_id;
        $recipient_id = (int)$recipient_id;
        $file_key_safe = $file_key ? "'" . VDatabase::escape($file_key) . "'" : 'NULL';
        $amount = (float)$amount;
        $message_safe = VDatabase::escape($message);

        // Process payment (Stripe integration here)
        $payment_id = self::processStripePayment($usr_id, $amount);

        if (!$payment_id) {
            return false;
        }

        $payment_safe = VDatabase::escape($payment_id);

        $sql = "INSERT INTO db_super_chats
                (usr_id, recipient_id, file_key, amount, currency, message, type, payment_status, stripe_payment_id, created_at)
                VALUES ($usr_id, $recipient_id, $file_key_safe, $amount, 'USD', '$message_safe', 'super_chat', 'completed', '$payment_safe', NOW())";

        if (self::$db->execute($sql)) {
            // Record transaction
            self::recordTransaction($recipient_id, 'super_chat', $amount, "Super Chat from user $usr_id", $payment_id);
            return self::$db->insert_id();
        }

        return false;
    }

    /**
     * Process Stripe payment (simplified)
     */
    private static function processStripePayment($usr_id, $amount) {
        // Placeholder for Stripe payment processing
        // In production, use Stripe PHP SDK to create PaymentIntent
        return 'pi_' . bin2hex(random_bytes(16));
    }

    /**
     * Record transaction
     */
    private static function recordTransaction($usr_id, $type, $amount, $description, $reference_id = null) {
        $usr_id = (int)$usr_id;
        $type_safe = VDatabase::escape($type);
        $amount = (float)$amount;
        $desc_safe = VDatabase::escape($description);
        $ref_safe = $reference_id ? "'" . VDatabase::escape($reference_id) . "'" : 'NULL';

        $sql = "INSERT INTO db_transactions
                (usr_id, type, amount, currency, description, reference_id, status, created_at)
                VALUES ($usr_id, '$type_safe', $amount, 'USD', '$desc_safe', $ref_safe, 'completed', NOW())";

        return self::$db->execute($sql);
    }

    /**
     * Calculate revenue share
     * @param int $usr_id User ID
     * @param string $period_start Start date
     * @param string $period_end End date
     * @return array Revenue breakdown
     */
    public static function calculateRevenue($usr_id, $period_start, $period_end) {
        self::init();

        $usr_id = (int)$usr_id;
        $start_safe = VDatabase::escape($period_start);
        $end_safe = VDatabase::escape($period_end);

        // Get all revenue streams
        $sql = "SELECT type, SUM(amount) as total
                FROM db_transactions
                WHERE usr_id = $usr_id
                AND status = 'completed'
                AND created_at BETWEEN '$start_safe' AND '$end_safe'
                GROUP BY type";

        $result = self::$db->execute($sql);

        $revenue = [
            'ad_revenue' => 0,
            'membership_revenue' => 0,
            'super_chat_revenue' => 0,
            'total_revenue' => 0
        ];

        if ($result) {
            while ($row = $result->FetchRow()) {
                $amount = (float)$row['total'];
                $revenue['total_revenue'] += $amount;

                if ($row['type'] == 'membership') {
                    $revenue['membership_revenue'] = $amount;
                } elseif ($row['type'] == 'super_chat' || $row['type'] == 'super_thanks') {
                    $revenue['super_chat_revenue'] += $amount;
                } elseif ($row['type'] == 'ad_payout') {
                    $revenue['ad_revenue'] = $amount;
                }
            }
        }

        // Calculate platform fee (e.g., 30%)
        $platform_fee = $revenue['total_revenue'] * 0.30;
        $payout_amount = $revenue['total_revenue'] - $platform_fee;

        $revenue['platform_fee'] = $platform_fee;
        $revenue['payout_amount'] = $payout_amount;

        return $revenue;
    }

    /**
     * Create revenue share record
     * @param int $usr_id User ID
     * @param string $period_start Start date
     * @param string $period_end End date
     * @return int Share ID
     */
    public static function createRevenueShare($usr_id, $period_start, $period_end) {
        $revenue = self::calculateRevenue($usr_id, $period_start, $period_end);

        $usr_id = (int)$usr_id;
        $start_safe = VDatabase::escape($period_start);
        $end_safe = VDatabase::escape($period_end);

        $sql = "INSERT INTO db_revenue_shares
                (usr_id, period_start, period_end, ad_revenue, membership_revenue, super_chat_revenue, total_revenue, platform_fee, payout_amount, payout_status, created_at)
                VALUES ($usr_id, '$start_safe', '$end_safe', {$revenue['ad_revenue']}, {$revenue['membership_revenue']}, {$revenue['super_chat_revenue']},
                        {$revenue['total_revenue']}, {$revenue['platform_fee']}, {$revenue['payout_amount']}, 'pending', NOW())";

        if (self::$db->execute($sql)) {
            return self::$db->insert_id();
        }

        return false;
    }

    /**
     * Get user memberships
     * @param int $usr_id User ID
     * @return array Active memberships
     */
    public static function getUserMemberships($usr_id) {
        self::init();

        $usr_id = (int)$usr_id;

        $sql = "SELECT m.*, t.name as tier_name, t.price_monthly, t.perks, u.usr_user as channel_name
                FROM db_memberships m
                JOIN db_membership_tiers t ON m.tier_id = t.tier_id
                JOIN db_accountuser u ON m.channel_owner_id = u.usr_id
                WHERE m.subscriber_id = $usr_id
                AND m.status = 'active'
                ORDER BY m.started_at DESC";

        $result = self::$db->execute($sql);
        $memberships = [];

        if ($result) {
            while ($row = $result->FetchRow()) {
                $row['perks'] = json_decode($row['perks'], true);
                $memberships[] = $row;
            }
        }

        return $memberships;
    }

    /**
     * Cancel membership
     * @param int $membership_id Membership ID
     * @return bool Success
     */
    public static function cancelMembership($membership_id) {
        self::init();

        $membership_id = (int)$membership_id;

        $sql = "UPDATE db_memberships
                SET status = 'cancelled', cancelled_at = NOW()
                WHERE membership_id = $membership_id";

        return self::$db->execute($sql);
    }
}
