<?php
/**
 * Plugin Name: Divine Agreement System
 * Plugin URI: 
 * Description: A complete agreement management system with templates, digital signatures, PDF generation, and email notifications.
 * Version: 1.0.4
 * Author: Aditya Singh
 * Author URI:
 * Text Domain: divine-agreement-system
 * Requires at least: 5.8
 * Requires PHP: 7.4
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */

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


define('DIVINE_AGREEMENT_VERSION', '1.0.4');
define('DIVINE_AGREEMENT_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('DIVINE_AGREEMENT_PLUGIN_URL', plugin_dir_url(__FILE__));
define('DIVINE_AGREEMENT_PLUGIN_BASENAME', plugin_basename(__FILE__));


$divine_agreement_upload_dir = wp_upload_dir();

define('DIVINE_AGREEMENT_UPLOAD_DIR', $divine_agreement_upload_dir['basedir'] . '/divine-agreements/');
define('DIVINE_AGREEMENT_UPLOAD_URL', $divine_agreement_upload_dir['baseurl'] . '/divine-agreements/');

class Divine_Agreement_System {

    private static $instance = null;

    public static function get_instance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        $this->load_dependencies();
        $this->init_hooks();
    }

    
    private function load_dependencies() {

        if (file_exists(DIVINE_AGREEMENT_PLUGIN_DIR . 'vendor/autoload.php')) {
            require_once DIVINE_AGREEMENT_PLUGIN_DIR . 'vendor/autoload.php';
        }

        require_once DIVINE_AGREEMENT_PLUGIN_DIR . 'includes/divine-post-types.php';
        require_once DIVINE_AGREEMENT_PLUGIN_DIR . 'includes/divine-token.php';
        require_once DIVINE_AGREEMENT_PLUGIN_DIR . 'includes/divine-placeholder-engine.php';
        require_once DIVINE_AGREEMENT_PLUGIN_DIR . 'includes/divine-security.php';
        require_once DIVINE_AGREEMENT_PLUGIN_DIR . 'includes/divine-email.php';
        require_once DIVINE_AGREEMENT_PLUGIN_DIR . 'includes/divine-pdf-generator.php';
        require_once DIVINE_AGREEMENT_PLUGIN_DIR . 'includes/divine-admin-actions.php';
    if (file_exists(DIVINE_AGREEMENT_PLUGIN_DIR . 'includes/integrations/class-divine-form-integration.php')) {
        require_once DIVINE_AGREEMENT_PLUGIN_DIR . 'includes/integrations/class-divine-form-integration.php';
    }
        require_once DIVINE_AGREEMENT_PLUGIN_DIR . 'includes/divine-donation.php';

    }

    
    private function init_hooks() {

        add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend_assets']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_assets']);
        add_action('template_redirect', [$this, 'template_redirect']);
        
        if (class_exists('Divine_Form_Integration')) {
            Divine_Form_Integration::init();
        }
    }

 
    public static function activate() {

        $upload_dir = wp_upload_dir();
        $dir = $upload_dir['basedir'] . '/divine-agreements/';

        if (!file_exists($dir)) {
            wp_mkdir_p($dir);
        }

        Divine_Post_Types::divine_register_post_types();
        flush_rewrite_rules();
    }

    public static function deactivate() {
        flush_rewrite_rules();
    }


    public function enqueue_frontend_assets() {

        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Public signing links intentionally use a tokenized GET parameter.
        if (isset($_GET['token'])) {
            // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Public signing links intentionally use a tokenized GET parameter.
            $token = sanitize_text_field(wp_unslash($_GET['token']));

            if ($token === '') {
                return;
            }

            wp_enqueue_script(
                'divine-signature-js',
                DIVINE_AGREEMENT_PLUGIN_URL . 'assets/js/divine-signature.js',
                ['jquery'],
                DIVINE_AGREEMENT_VERSION,
                true
            );

            wp_enqueue_style(
                'divine-agreement-css',
                DIVINE_AGREEMENT_PLUGIN_URL . 'assets/css/divine-style.css',
                [],
                DIVINE_AGREEMENT_VERSION
            );

            wp_localize_script('divine-signature-js', 'divineAgreementData', [
                'ajaxUrl' => admin_url('admin-ajax.php'),
                'nonce'   => wp_create_nonce('divine_agreement_nonce')
            ]);
        }
    }

    public function enqueue_admin_assets($hook) {

        $screen = get_current_screen();

        if ($screen && (
            $screen->post_type === 'divine_agreement' ||
            $screen->post_type === 'divine_template'
        )) {

            wp_enqueue_style(
                'divine-admin-css',
                DIVINE_AGREEMENT_PLUGIN_URL . 'assets/css/divine-style.css',
                [],
                DIVINE_AGREEMENT_VERSION
            );
        }
    }


    public function template_redirect() {

        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Public signing links intentionally use a tokenized GET parameter.
        if (isset($_GET['token'])) {
            // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Public signing links intentionally use a tokenized GET parameter.
            $token = sanitize_text_field(wp_unslash($_GET['token']));

            if ($token === '') {
                return;
            }

            $agreement_id = Divine_Token::divine_validate_token($token);

            if ($agreement_id) {
                include DIVINE_AGREEMENT_PLUGIN_DIR . 'templates/divine-signing-page.php';
                exit;
            }

            wp_die(
                esc_html__('Invalid or expired agreement link.', 'divine-agreement-system'),
                esc_html__('Invalid Link', 'divine-agreement-system'),
                ['response' => 403]
            );
        }
    }
}


function divine_agreement_system_init() {
    return Divine_Agreement_System::get_instance();
}

register_activation_hook(__FILE__, ['Divine_Agreement_System', 'activate']);
register_deactivation_hook(__FILE__, ['Divine_Agreement_System', 'deactivate']);

divine_agreement_system_init();
