<?php
/**
 * 
 * @package Divine_Agreement_System
 * @author Aditya Singh
 */
if (!defined('ABSPATH')) {
    exit;
}

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

if (!$divine_agreement_id) {
    wp_die(esc_html__('Invalid or expired agreement link.', 'divine-agreement-system'));
}

$divine_agreement = get_post($divine_agreement_id);
$divine_data = Divine_Placeholder_Engine::divine_get_agreement_data($divine_agreement_id);
$divine_content = Divine_Placeholder_Engine::divine_get_rendered_content($divine_agreement_id);


if ($divine_data['status'] === 'signed') {
    wp_die(esc_html__('This agreement has already been signed.', 'divine-agreement-system'));
}

get_header();
?>

<div class="divine-agreement-system-wrapper">
    <div class="divine-agreement-system-container">
        <div class="divine-agreement-system-header">
            <h1><?php echo esc_html($divine_agreement->post_title); ?></h1>
            <p class="divine-agreement-system-subtitle">
                <?php esc_html_e('Please review the agreement below and sign digitally', 'divine-agreement-system'); ?>
            </p>
        </div>
        
        <div class="divine-agreement-system-content">
            <?php echo wp_kses_post($divine_content); ?>
        </div>
        
        <div class="divine-signature-section">
            <h2><?php esc_html_e('Digital Signature', 'divine-agreement-system'); ?></h2>
            
            <form id="divine-signature-form" class="divine-signature-form">
                <?php wp_nonce_field('divine_agreement_nonce', 'divine_signature_nonce'); ?>
                <div class="divine-form-row">
                    <label for="divine_name">
                        <?php esc_html_e('Full Name', 'divine-agreement-system'); ?> <span class="required">*</span>
                    </label>
                    <input type="text" 
                           id="divine_name" 
                           name="divine_name" 
                           value="<?php echo esc_attr($divine_data['collaborator_name']); ?>" 
                           required>
                </div>
                
                <div class="divine-form-row">
                    <label for="divine_email">
                        <?php esc_html_e('Email Address', 'divine-agreement-system'); ?> <span class="required">*</span>
                    </label>
                    <input type="email" 
                           id="divine_email" 
                           name="divine_email" 
                           value="<?php echo esc_attr($divine_data['collaborator_email']); ?>" 
                           required>
                </div>
                
                <div class="divine-form-row">
                    <label for="divine_address">
                        <?php esc_html_e('Address', 'divine-agreement-system'); ?> <span class="required">*</span>
                    </label>
                    <textarea id="divine_address" 
                              name="divine_address" 
                              rows="3" 
                              required><?php echo esc_textarea($divine_data['collaborator_address']); ?></textarea>
                </div>
                
                <div class="divine-form-row">
                    <label>
                        <?php esc_html_e('Draw Your Signature', 'divine-agreement-system'); ?> <span class="required">*</span>
                    </label>
                    <div class="divine-signature-pad-wrapper">
                        <canvas id="divine-signature-pad" width="600" height="200"></canvas>
                        <button type="button" id="divine-clear-signature" class="divine-clear-btn">
                            <?php esc_html_e('Clear Signature', 'divine-agreement-system'); ?>
                        </button>
                    </div>
                    <p class="divine-signature-note">
                        <?php esc_html_e('Please sign above using your mouse or touchscreen', 'divine-agreement-system'); ?>
                    </p>
                </div>
                
                <div class="divine-form-row divine-acceptance-row">
                    <label class="divine-checkbox-label">
                        <input type="checkbox" 
                               id="divine_acceptance" 
                               name="divine_acceptance" 
                               value="yes" 
                               required>
                        <span>
                            <?php esc_html_e('I have read, understood, and agree to the terms of this agreement', 'divine-agreement-system'); ?>
                            <span class="required">*</span>
                        </span>
                    </label>
                </div>
                
                <div class="divine-form-row divine-submit-row">
                    <button type="submit" id="divine-submit-btn" class="divine-submit-btn">
                        <?php esc_html_e('Sign Agreement', 'divine-agreement-system'); ?>
                    </button>
                    <div id="divine-loading" class="divine-loading" style="display:none;">
                        <?php esc_html_e('Processing...', 'divine-agreement-system'); ?>
                    </div>
                </div>
                
                <div id="divine-message" class="divine-message"></div>
                
                <input type="hidden" id="divine_token" name="divine_token" value="<?php echo esc_attr($divine_token); ?>">
                <input type="hidden" id="divine_signature_data" name="divine_signature_data">
            </form>
        </div>
        
        <div class="divine-agreement-system-footer">
            <p>
                <span><?php esc_html_e('Powered by', 'divine-agreement-system'); ?></span>
                <strong><?php echo esc_html(get_bloginfo('name')); ?></strong>
            </p>
        </div>
    </div>
</div>

<script>

var divineSigningData = {
    token: '<?php echo esc_js($divine_token); ?>',
    agreementId: <?php echo intval($divine_agreement_id); ?>
};
</script>

<?php
get_footer();
