<?php
/**
 * @package Divine_Agreement_System
 * @author Aditya Singh
 */


if (!defined('ABSPATH')) {
    exit;
}

class Divine_Admin_Actions {
    
    
    public static function init() {
        
        add_action('admin_post_divine_send_agreement_email', array(__CLASS__, 'divine_handle_send_email'));
        add_action('admin_post_divine_download_pdf', array(__CLASS__, 'divine_handle_download_pdf'));
        
        
        add_filter('bulk_actions-edit-divine_agreement', array(__CLASS__, 'divine_register_bulk_actions'));
        add_filter('handle_bulk_actions-edit-divine_agreement', array(__CLASS__, 'divine_handle_bulk_actions'), 10, 3);
        
        
        add_action('admin_notices', array(__CLASS__, 'divine_admin_notices'));
    }
    
    
    public static function divine_handle_send_email() {
        if (!current_user_can('edit_posts')) {
            wp_die(esc_html__('You do not have permission to perform this action.', 'divine-agreement-system'));
        }

        check_admin_referer('divine_send_agreement_email');

        $agreement_id = isset($_GET['agreement_id']) ? absint(wp_unslash($_GET['agreement_id'])) : 0;

        if (!$agreement_id) {
            wp_die(esc_html__('Invalid agreement ID.', 'divine-agreement-system'));
        }

        $sent = Divine_Email::divine_send_agreement_email($agreement_id);

        $redirect_url = admin_url('post.php?post=' . $agreement_id . '&action=edit');

        if ($sent) {
            $redirect_url = add_query_arg('divine_email_sent', '1', $redirect_url);
        } else {
            $redirect_url = add_query_arg('divine_email_error', '1', $redirect_url);
        }

        wp_safe_redirect($redirect_url);
        exit;
    }
    
    
    public static function divine_handle_download_pdf() {
        check_admin_referer('divine_download_pdf');

        $agreement_id = isset($_GET['agreement_id']) ? absint(wp_unslash($_GET['agreement_id'])) : 0;

        if (!$agreement_id) {
            wp_die(esc_html__('Invalid agreement ID.', 'divine-agreement-system'));
        }
        
        Divine_PDF_Generator::divine_download_pdf($agreement_id);
    }
    
    
    public static function divine_register_bulk_actions($bulk_actions) {
        $bulk_actions['divine_send_emails'] = esc_html__('Send Divine Agreement Emails', 'divine-agreement-system');
        return $bulk_actions;
    }
    
    
    public static function divine_handle_bulk_actions($redirect_to, $doaction, $post_ids) {
        if ($doaction !== 'divine_send_emails') {
            return $redirect_to;
        }
        
        $sent_count = 0;
        
        foreach ($post_ids as $post_id) {
            
            $status = get_post_meta($post_id, '_divine_status', true);
            
            if ($status !== 'signed') {
                $sent = Divine_Email::divine_send_agreement_email($post_id);
                if ($sent) {
                    $sent_count++;
                }
            }
        }
        
        $redirect_to = add_query_arg('divine_bulk_emails_sent', $sent_count, $redirect_to);
        
        return $redirect_to;
    }
    
    
    public static function divine_admin_notices() {
        
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Admin notice flags are read-only query args.
        $divine_email_sent = isset($_GET['divine_email_sent']) ? sanitize_text_field(wp_unslash($_GET['divine_email_sent'])) : '';
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Admin notice flags are read-only query args.
        $divine_email_error = isset($_GET['divine_email_error']) ? sanitize_text_field(wp_unslash($_GET['divine_email_error'])) : '';
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Admin notice flags are read-only query args.
        $divine_bulk_emails_sent = isset($_GET['divine_bulk_emails_sent']) ? absint(wp_unslash($_GET['divine_bulk_emails_sent'])) : 0;
        $divine_has_bulk_notice = false;

        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Admin notice flags are read-only query args.
        if (isset($_GET['divine_bulk_emails_sent'])) {
            $divine_has_bulk_notice = true;
        }

        if ('1' === $divine_email_sent) {
            ?>
            <div class="notice notice-success is-dismissible">
                <p><?php esc_html_e('Divine Agreement email sent successfully!', 'divine-agreement-system'); ?></p>
            </div>
            <?php
        }
        
       
        if ('1' === $divine_email_error) {
            ?>
            <div class="notice notice-error is-dismissible">
                <p><?php esc_html_e('Failed to send email. Please check your email configuration.', 'divine-agreement-system'); ?></p>
            </div>
            <?php
        }
        
        
        if ($divine_has_bulk_notice) {
            $count = $divine_bulk_emails_sent;
            ?>
            <div class="notice notice-success is-dismissible">
                <p>
                    <?php
                    /* translators: %d: Number of Divine Agreement emails sent. */
                    echo esc_html(sprintf(esc_html__('Divine Agreement emails sent: %d', 'divine-agreement-system'), $count));
                    ?>
                </p>
            </div>
            <?php
        }
    }
}


Divine_Admin_Actions::init();
