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


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

class Divine_Placeholder_Engine {
    
    public static function divine_replace_placeholders($content, $agreement_id) {
        
        $collaborator_name = get_post_meta($agreement_id, '_divine_collaborator_name', true);
        $collaborator_email = get_post_meta($agreement_id, '_divine_collaborator_email', true);
        $collaborator_address = get_post_meta($agreement_id, '_divine_collaborator_address', true);
        $compensation = get_post_meta($agreement_id, '_divine_compensation', true);
        $signed_date = get_post_meta($agreement_id, '_divine_signed_date', true);
        
        
        $current_date = date_i18n(get_option('date_format'));
        $agreement_date = $signed_date ? date_i18n(get_option('date_format'), strtotime($signed_date)) : $current_date;
        
        
        $placeholders = array(
            '[COLLABORATOR_NAME]' => $collaborator_name,
            '[COLLABORATOR_EMAIL]' => $collaborator_email,
            '[COLLABORATOR_ADDRESS]' => nl2br($collaborator_address),
            '[COMPENSATION_DETAILS]' => nl2br($compensation),
            '[DATE]' => $agreement_date,
            '[CURRENT_DATE]' => $current_date
        );
        
        
        $placeholders = apply_filters('divine_agreement_placeholders', $placeholders, $agreement_id);
        
        
        foreach ($placeholders as $placeholder => $value) {
            $content = str_replace($placeholder, $value, $content);
        }
        
        return $content;
    }
    
    
    public static function divine_get_rendered_content($agreement_id) {
        
        $template_id = get_post_meta($agreement_id, '_divine_template_id', true);
        
        if (!$template_id) {
            return __('No template selected.', 'divine-agreement-system');
        }
        
        $template = get_post($template_id);
        
        if (!$template) {
            return __('Template not found.', 'divine-agreement-system');
        }
        
        
        $content = $template->post_content;
        
        
        // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Core WordPress content filter.
        $content = apply_filters('the_content', $content);
        
        
        $content = self::divine_replace_placeholders($content, $agreement_id);
        
        return $content;
    }
    
    
    public static function divine_get_agreement_data($agreement_id) {
        return array(
            'collaborator_name' => get_post_meta($agreement_id, '_divine_collaborator_name', true),
            'collaborator_email' => get_post_meta($agreement_id, '_divine_collaborator_email', true),
            'collaborator_address' => get_post_meta($agreement_id, '_divine_collaborator_address', true),
            'compensation' => get_post_meta($agreement_id, '_divine_compensation', true),
            'status' => get_post_meta($agreement_id, '_divine_status', true),
            'signed_date' => get_post_meta($agreement_id, '_divine_signed_date', true),
            'signed_ip' => get_post_meta($agreement_id, '_divine_signed_ip', true),
            'signature_image' => get_post_meta($agreement_id, '_divine_signature_image', true),
            'pdf_path' => get_post_meta($agreement_id, '_divine_pdf_path', true),
            'token' => get_post_meta($agreement_id, '_divine_token', true)
        );
    }
}
