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


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

class Divine_Email {
    
    public static function divine_send_agreement_email($agreement_id) {
        $agreement = get_post($agreement_id);
        
        if (!$agreement) {
            return false;
        }
        
        
        $collaborator_email = get_post_meta($agreement_id, '_divine_collaborator_email', true);
        $collaborator_name = get_post_meta($agreement_id, '_divine_collaborator_name', true);
        $token = get_post_meta($agreement_id, '_divine_token', true);
        
        if (!$collaborator_email || !$token) {
            return false;
        }
        
        
        $signing_url = Divine_Token::divine_get_signing_url($token);
        
        
        $subject = sprintf(
            /* translators: %s: Site name. */
            __('[%1$s] Please Sign Your Agreement', 'divine-agreement-system'),
            get_bloginfo('name')
        );
        
        
        $message = sprintf(
            /* translators: %s: Collaborator name. */
            __('Hello %1$s,', 'divine-agreement-system'),
            $collaborator_name
        ) . "\n\n";
        
        $message .= __('You have been sent an agreement to review and sign.', 'divine-agreement-system') . "\n\n";
        $message .= __('Agreement Title:', 'divine-agreement-system') . ' ' . $agreement->post_title . "\n\n";
        $message .= __('To review and sign this agreement, please click the link below:', 'divine-agreement-system') . "\n\n";
        $message .= $signing_url . "\n\n";
        $message .= __('This link is unique to you and should not be shared.', 'divine-agreement-system') . "\n\n";
        $message .= __('Best regards,', 'divine-agreement-system') . "\n";
        $message .= get_bloginfo('name');
        
        
        $headers = array(
            'Content-Type: text/plain; charset=UTF-8',
            'From: ' . get_bloginfo('name') . ' <' . get_option('admin_email') . '>'
        );
        
        
        $sent = wp_mail($collaborator_email, $subject, $message, $headers);
        
        
        if ($sent) {
            update_post_meta($agreement_id, '_divine_email_sent', current_time('mysql'));
        }
        
        return $sent;
    }
    
    
    public static function divine_send_signed_notification($agreement_id) {
        $agreement = get_post($agreement_id);
        
        if (!$agreement) {
            return false;
        }
        
        
        $collaborator_email = get_post_meta($agreement_id, '_divine_collaborator_email', true);
        $collaborator_name = get_post_meta($agreement_id, '_divine_collaborator_name', true);
        $pdf_path = get_post_meta($agreement_id, '_divine_pdf_path', true);
        
        
        $attachments = array();
        if ($pdf_path && file_exists($pdf_path)) {
            $attachments[] = $pdf_path;
        }
        
        
        self::divine_send_user_signed_email($agreement_id, $collaborator_email, $collaborator_name, $attachments);
        
        
        self::divine_send_admin_signed_email($agreement_id, $attachments);
        
        return true;
    }
    
    
    private static function divine_send_user_signed_email($agreement_id, $email, $name, $attachments) {
        $agreement = get_post($agreement_id);
        
        
        $subject = sprintf(
            /* translators: %s: Site name. */
            __('[%1$s] Your Signed Agreement', 'divine-agreement-system'),
            get_bloginfo('name')
        );
        
        
        $message = sprintf(
            /* translators: %s: Collaborator name. */
            __('Hello %1$s,', 'divine-agreement-system'),
            $name
        ) . "\n\n";
        $message .= __('Thank you for signing your agreement!', 'divine-agreement-system') . "\n\n";
        $message .= __('Agreement:', 'divine-agreement-system') . ' ' . $agreement->post_title . "\n";
        $message .= __('Signed on:', 'divine-agreement-system') . ' ' . date_i18n(get_option('date_format') . ' ' . get_option('time_format')) . "\n\n";
        $message .= __('A copy of your signed agreement is attached to this email for your records.', 'divine-agreement-system') . "\n\n";
        $message .= __('Best regards,', 'divine-agreement-system') . "\n";
        $message .= get_bloginfo('name');
        
        
        $headers = array(
            'Content-Type: text/plain; charset=UTF-8',
            'From: ' . get_bloginfo('name') . ' <' . get_option('admin_email') . '>'
        );
        
        wp_mail($email, $subject, $message, $headers, $attachments);
    }
    
    
    private static function divine_send_admin_signed_email($agreement_id, $attachments) {
        $agreement = get_post($agreement_id);
        $collaborator_name = get_post_meta($agreement_id, '_divine_collaborator_name', true);
        $collaborator_email = get_post_meta($agreement_id, '_divine_collaborator_email', true);
        $signed_date = get_post_meta($agreement_id, '_divine_signed_date', true);
        
        
        $subject = sprintf(
            /* translators: 1: Site name, 2: Collaborator name. */
            __('[%1$s] Agreement Signed by %2$s', 'divine-agreement-system'),
            get_bloginfo('name'),
            $collaborator_name
        );
        
        
        $message = __('An agreement has been signed:', 'divine-agreement-system') . "\n\n";
        $message .= __('Agreement:', 'divine-agreement-system') . ' ' . $agreement->post_title . "\n";
        $message .= __('Signed by:', 'divine-agreement-system') . ' ' . $collaborator_name . "\n";
        $message .= __('Email:', 'divine-agreement-system') . ' ' . $collaborator_email . "\n";
        $message .= __('Signed on:', 'divine-agreement-system') . ' ' . date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($signed_date)) . "\n\n";
        $message .= __('View agreement:', 'divine-agreement-system') . ' ' . admin_url('post.php?post=' . $agreement_id . '&action=edit') . "\n\n";
        $message .= __('The signed PDF is attached to this email.', 'divine-agreement-system');
        
        
        $headers = array(
            'Content-Type: text/plain; charset=UTF-8'
        );
        
        wp_mail(get_option('admin_email'), $subject, $message, $headers, $attachments);
    }
    
    
    public static function divine_get_email_template($type, $data) {
        $template = '';
        
        switch ($type) {
            case 'signing_request':
                $template = self::divine_signing_request_template($data);
                break;
                
            case 'signed_confirmation':
                $template = self::divine_signed_confirmation_template($data);
                break;
        }
        
        return apply_filters('divine_email_template', $template, $type, $data);
    }
    
    
    private static function divine_signing_request_template($data) {
        ob_start();
        ?>
        <p>Hello <?php echo esc_html($data['name']); ?>,</p>
        <p>You have been sent an agreement to review and sign.</p>
        <p><strong>Agreement:</strong> <?php echo esc_html($data['title']); ?></p>
        <p><a href="<?php echo esc_url($data['url']); ?>" style="display:inline-block;padding:10px 20px;background:#0073aa;color:#fff;text-decoration:none;">Review and Sign Agreement</a></p>
        <p>This link is unique to you and should not be shared.</p>
        <p>Best regards,<br><?php echo esc_html(get_bloginfo('name')); ?></p>
        <?php
        return ob_get_clean();
    }
    
    
    private static function divine_signed_confirmation_template($data) {
        ob_start();
        ?>
        <p>Hello <?php echo esc_html($data['name']); ?>,</p>
        <p>Thank you for signing your agreement!</p>
        <p><strong>Agreement:</strong> <?php echo esc_html($data['title']); ?></p>
        <p><strong>Signed on:</strong> <?php echo esc_html($data['date']); ?></p>
        <p>A copy of your signed agreement is attached to this email for your records.</p>
        <p>Best regards,<br><?php echo esc_html(get_bloginfo('name')); ?></p>
        <?php
        return ob_get_clean();
    }
}
