<?php
if (!defined('ABSPATH')) {
    exit;
}

class Divine_Donation {

    const UPI_ID = 'aditya4058@ptyes';
    const PAYPAL_LINK = 'https://www.paypal.com/paypalme/aditya2752';

    public static function init() {

        add_filter('plugin_row_meta', array(__CLASS__, 'divine_plugin_row_meta'), 10, 2);
        add_action('admin_notices', array(__CLASS__, 'divine_donation_notice'));
        add_action('wp_ajax_divine_dismiss_donation_notice', array(__CLASS__, 'divine_dismiss_donation_notice'));
        add_action('admin_enqueue_scripts', array(__CLASS__, 'divine_enqueue_donation_assets'));
    }

    public static function divine_plugin_row_meta($links, $file) {
        if ($file === DIVINE_AGREEMENT_PLUGIN_BASENAME) {
            $donate_links = array(
                'donate_upi' => '<a href="#" class="divine-upi-donate-trigger" style="color:#FF6B6B;font-weight:bold;">Donate via UPI</a>',
                'donate_paypal' => '<a href="' . esc_url(self::PAYPAL_LINK) . '" target="_blank" rel="noopener noreferrer" style="color:#0070BA;font-weight:bold;">Donate via PayPal</a>',
            );
            $links = array_merge($links, $donate_links);
        }
        return $links;
    }

    public static function divine_donation_notice() {

        $screen = get_current_screen();

        if (!$screen || !in_array($screen->post_type, array('divine_template', 'divine_agreement'), true)) {
            return;
        }

        ?>
        <div class="notice notice-info divine-donation-notice">
            <button type="button" class="divine-close-notice" style="float:right;background:none;border:none;font-size:18px;cursor:pointer;">&times;</button>

            <div class="divine-donation-content">
                <div class="divine-donation-icon">&#128150;</div>
                <div class="divine-donation-text">
                    <h3>Love Divine Agreement System?</h3>
                    <p>
                        This plugin is <strong>100% FREE</strong>.
                        If it saved you time or money, please consider supporting development.
                    </p>
                </div>
                <div class="divine-donation-buttons">
                    <a href="#" class="button button-primary divine-upi-donate-trigger">
                        &#128179; Donate via UPI
                    </a>
                    <a href="<?php echo esc_url(self::PAYPAL_LINK); ?>" target="_blank" rel="noopener noreferrer" class="button button-secondary">
                        &#128176; Donate via PayPal
                    </a>
                </div>
            </div>
        </div>
        <?php
    }

    public static function divine_enqueue_donation_assets($hook) {

        $screen = get_current_screen();
        $load_assets = false;

        if ($hook === 'plugins.php') {
            $load_assets = true;
        }

        if ($screen && in_array($screen->post_type, array('divine_template', 'divine_agreement'), true)) {
            $load_assets = true;
        }

        if (!$load_assets) {
            return;
        }

        wp_enqueue_script('jquery');

        wp_register_style('divine-donation-inline', false, array(), DIVINE_AGREEMENT_VERSION);
        wp_enqueue_style('divine-donation-inline');
        wp_add_inline_style('divine-donation-inline', self::divine_get_donation_css());

        wp_add_inline_script('jquery', self::divine_get_donation_js());
    }

    private static function divine_get_donation_css() {
        return "
        .divine-donation-notice{border-left:4px solid #FF6B6B;padding:15px;position:relative}
        .divine-donation-content{display:flex;align-items:center;gap:20px}
        .divine-donation-icon{font-size:40px}
        .divine-donation-buttons{display:flex;gap:10px}
        .divine-upi-modal-overlay{position:fixed;inset:0;background:rgba(0,0,0,.8);z-index:999999;display:none;align-items:center;justify-content:center}
        .divine-upi-modal-overlay.active{display:flex}
        .divine-upi-modal{background:#fff;border-radius:12px;max-width:500px;width:90%;padding:30px;text-align:center}
        .divine-upi-qr-code{max-width:250px;width:100%;margin-bottom:20px}
        ";
    }

    private static function divine_get_donation_js() {

        $upi_id = self::UPI_ID;
        $qr_code_url = DIVINE_AGREEMENT_PLUGIN_URL . 'assets/images/divine-upi-qr.png';

        return "
        jQuery(document).ready(function($){

            if(!$('.divine-upi-modal-overlay').length){
                $('body').append(
                    '<div class=\"divine-upi-modal-overlay\">'+
                        '<div class=\"divine-upi-modal\">'+
                            '<h2>Support Development &#128150;</h2>'+
                            '<img src=\"{$qr_code_url}\" class=\"divine-upi-qr-code\">'+
                            '<p><strong>UPI ID:</strong> {$upi_id}</p>'+
                            '<button class=\"button divine-close-modal-btn\">Close</button>'+
                        '</div>'+
                    '</div>'
                );
            }

            $(document).on('click','.divine-upi-donate-trigger',function(e){
                e.preventDefault();
                $('.divine-upi-modal-overlay').addClass('active');
            });

            $(document).on('click','.divine-close-modal-btn, .divine-upi-modal-overlay',function(e){
                if(e.target === this){
                    $('.divine-upi-modal-overlay').removeClass('active');
                }
            });

            $(document).on('click','.divine-upi-modal',function(e){
                e.stopPropagation();
            });

            $(document).on('click','.divine-close-notice',function(){
                $('.divine-donation-notice').slideUp();
            });

        });
        ";
    }

    public static function divine_dismiss_donation_notice() {
        wp_send_json_success();
    }
}

Divine_Donation::init();
