<?php

namespace PWH_DCFH\App\Frontend\Request;

if (!defined('ABSPATH')) {
    exit;
}
if (!class_exists('PWH_DCFH_Post_Repository')) {
    class PWH_DCFH_Post_Repository
    {
        private static $_instance;

        public $contact_form_id;

        public $contact_form_data = [];

        public $contact_form_fields = [];

        public $contact_form_fields_str;

        public $sender_email;

        public $contact_form_title;

        public $entry_number;

        public $save_entry_to_db = false;

        public $save_files_to_db = false;

        public $save_files_to_media = false;

        public $send_files_as_attachment = false;

        public $collect_ip_useragent_details = false;

        public $save_queryparams_to_db = false;

        public $attached_files_message;

        public $is_zapier_enabled = false;

        public $zapier_mailbox_address;

        public $is_pabbly_enabled = false;

        public $pabbly_mailbox_address;

        public $is_custom_message_richtext = false;

        public $module_admin_email;

        public $is_admin_email_enabled = false;

        public $admin_email_subject;

        public $admin_email_from_name;

        public $admin_email_cc;

        public $admin_email_bcc;

        public $is_admin_email_from_submitter_email = false;

        public $post_payload_endpoint;

        public $attachments = [];

        public $is_threshold_reached;

        public $is_googlerecaptcha_v2_enabled = false;

        public $is_cloudflare_turnstile_enabled = false;

        /**
         * Returns the singleton instance of the class.
         * @return self
         */
        public static function instance()
        {
            if (self::$_instance === null) {
                self::$_instance = new self();
            }

            return self::$_instance;
        }

        /**
         * Retrieves The Admin Email Subject
         * @return string
         * @since 1.0.0
         */
        public function get_admin_email_subject()
        {
            return $this->admin_email_subject;
        }

        /**
         * Retrieves The Admin Email From Name
         * @return string
         * @since 1.5
         */
        public function get_admin_email_from_name()
        {
            return $this->admin_email_from_name;
        }

        /**
         * Retrieves The Admin Email Cc
         * @return string
         * @since 1.1
         */
        public function get_admin_email_cc()
        {
            return $this->admin_email_cc;
        }

        /**
         * Retrieves The Admin Email Cc Headers
         *
         * @param $headers
         *
         * @return mixed
         */
        public function get_admin_cc_headers($headers)
        {
            $emails = $this->get_admin_email_cc();
            if (!empty($emails)) {
                $emails = preg_replace('/\s*,\s*/', ',', $emails);
                $emails = array_filter(explode(',', $emails));
                foreach ($emails as $email) {
                    $headers[] = sprintf('Cc: %s', sanitize_email($email));
                }
            }

            return $headers;
        }

        /**
         * Retrieves The Admin Email Bcc
         * @return string
         * @since 1.1
         */
        public function get_admin_email_bcc()
        {
            return $this->admin_email_bcc;
        }

        /**
         * Retrieves The Admin Email Bcc Headers
         *
         * @param $headers
         *
         * @return mixed
         */
        public function get_admin_bcc_headers($headers)
        {
            $emails = $this->get_admin_email_bcc();
            if (!empty($emails)) {
                $emails = preg_replace('/\s*,\s*/', ',', $emails);
                $emails = array_filter(explode(',', $emails));
                foreach ($emails as $email) {
                    $headers[] = sprintf('Bcc: %s', sanitize_email($email));
                }
            }

            return $headers;
        }
    }
}