<?php

namespace MovieEngine\Emails;

if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

abstract class EmailBase {
    protected $id;
    protected $title;
    protected $description;
    protected $template;
    protected $placeholders = [];
    protected $options;

    public function __construct() {
        $this->options = get_option('movie_engine_email_settings', []);
    }

    public function get_content($content_template, $data) {
        $this->placeholders = $data;
        ob_start();
        $this->load_template('header', $data);
        $this->load_template($content_template, $data);
        $this->load_template('footer', $data);
        $html = ob_get_clean();
        return $this->parse_placeholders($html);
    }

    protected function load_template($template_name, $data) {
        $file = MOVIE_ENGINE_PATH . 'templates/emails/' . $template_name . '.php';
        if (file_exists($file)) {
            $movie_engine_data = [];
            foreach ($data as $key => $value) {
                $movie_engine_data['movie_engine_' . $key] = $value;
            }
            extract($movie_engine_data);
            extract($data);
            include $file;
        }
    }

    protected function parse_placeholders($content) {
        foreach ($this->placeholders as $key => $value) {
            $content = str_replace('{' . $key . '}', $value, $content);
        }
        return $content;
    }

    public function send($to, $subject, $content_template, $data, $headers = []) {
        if (!isset($data['site_name'])) {
            $data['site_name'] = get_bloginfo('name');
        }

        $this->placeholders = $data;
        $notif_settings = isset($this->options['notifications'][$content_template]) ? $this->options['notifications'][$content_template] : [];

        if (isset($notif_settings['enabled']) && !$notif_settings['enabled']) {
            return false;
        }

        if (!empty($notif_settings['subject'])) {
            $subject = $this->parse_placeholders($notif_settings['subject']);
        }

        $body_content = '';
        if (!empty($notif_settings['content'])) {
            $body_content = wpautop($notif_settings['content']);
        } else {
            ob_start();
            $this->load_template($content_template, $data);
            $body_content = ob_get_clean();
        }

        ob_start();
        $this->load_template('header', $data);
        echo $body_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        $this->load_template('footer', $data);
        $full_body = ob_get_clean();
        $full_body = $this->parse_placeholders($full_body);

        if (empty($headers)) {
            $headers = ['Content-Type: text/html; charset=UTF-8'];
            $from_name = !empty($this->options['from_name']) ? $this->options['from_name'] : get_bloginfo('name');
            $from_email = !empty($this->options['from_email']) ? $this->options['from_email'] : get_option('admin_email');
            $reply_to  = !empty($this->options['reply_to']) ? $this->options['reply_to'] : $from_email;

            $from_name = str_replace(["\r", "\n", '<', '>'], '', $from_name);
            $from_email = sanitize_email($from_email);
            $reply_to = sanitize_email($reply_to);

            $headers[] = 'From: ' . $from_name . ' <' . $from_email . '>';
            $headers[] = 'Reply-To: <' . $reply_to . '>';
        }

        return wp_mail($to, $subject, $full_body, $headers);
    }
}
