<?php
class ControllerMailSubscription extends Controller {
    // admin/controller/sale/subscription/addHistory/after
    public function history(string &$route, array &$args, mixed &$output): void {
        if (isset($args[0])) {
            $subscription_id = $args[0];
        } else {
            $subscription_id = 0;
        }

        if (isset($args[1])) {
            $subscription_status_id = $args[1];
        } else {
            $subscription_status_id = 0;
        }

        if (isset($args[2])) {
            $comment = $args[2];
        } else {
            $comment = '';
        }

        if (isset($args[3])) {
            $notify = $args[3];
        } else {
            $notify = '';
        }

        // Subscription
        $this->load->model('sale/subscription');

        $filter_data = [
            'filter_subscription_id'        => $subscription_id,
            'filter_subscription_status_id' => $subscription_status_id,
            'filter_date_next'              => date('Y-m-d H:i:s')
        ];

        $subscriptions = $this->model_checkout_subscription->getSubscriptions($filter_data);

        if ($subscriptions) {
            foreach ($subscriptions as $subscription) {
                // Subscription histories
                $history_total = $this->model_sale_subscription->getTotalHistoriesBySubscriptionStatusId($subscription_status_id);

                // Requires an API to edit the subscription status from the catalog
                if ($history_total && $subscription['subscription_status_id'] == $subscription_status_id) {
                    // Subscription Statuses
                    $this->load->model('localisation/subscription_status');

                    $subscription_status_info = $this->model_localisation_subscription_status->getSubscriptionStatus($subscription_status_id);

                    if ($subscription_status_info) {
                        // Customers
                        $this->load->model('customer/customer');

                        // Customer payment
                        $customer_payment_info = $this->model_customer_customer->getPaymentMehod($subscription['customer_id'], $subscription['customer_payment_id']);

                        if ($customer_payment_info) {
                            $customer_info = $this->model_customer_customer->getCustomer($subscription['customer_id']);

                            if ($customer_info) {
                                // Settings
                                $this->load->model('setting/setting');

                                // Store
                                $store_info = $this->model_setting_setting->getSetting('config', $customer_info['store_id']);

                                if ($store_info) {
                                    $from = $store_info['config_email'];
                                    $store_name = $store_info['config_name'];
                                    $store_url = $store_info['config_url'];
                                    $alert_email = $store_info['config_mail_alert_email'];
                                } else {
                                    $from = $this->config->get('config_email');
                                    $store_name = $this->config->get('config_name');
                                    $store_url = HTTP_CATALOG;
                                    $alert_email = $this->config->get('config_mail_alert_email');
                                }

                                // Languages
                                $this->load->model('localisation/language');

                                $language_info = $this->model_localisation_language->getLanguage($customer_info['language_id']);

                                if ($language_info) {
                                    if ($comment && $notify) {
                                        $data['comment'] = nl2br($comment);
                                    } else {
                                        $data['comment'] = '';
                                    }

                                    $data['subscription_status'] = $subscription_status_info['name'];

                                    // Load the language for any mails that might be required to be sent out
                                    $language = new \Language($language_info['code']);
                                    $language->load($language_info['code']);
                                    $language->load('mail/subscription');

                                    $data['date_added'] = date($language->get('date_format_short'), $subscription['date_added']);

                                    // Text
                                    $data['text_comment'] = $language->get('text_comment');
                                    $data['text_date_added'] = $language->get('text_date_added');
                                    $data['text_footer'] = $language->get('text_footer');
                                    $data['text_subscription_status'] = $language->get('text_subscription_status');

                                    if ($this->config->get('config_mail_engine')) {
                                        $mail_option = [
                                            'parameter'     => $this->config->get('config_mail_parameter'),
                                            'smtp_hostname' => $this->config->get('config_mail_smtp_hostname'),
                                            'smtp_username' => $this->config->get('config_mail_smtp_username'),
                                            'smtp_password' => html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'),
                                            'smtp_port'     => $this->config->get('config_mail_smtp_port'),
                                            'smtp_timeout'  => $this->config->get('config_mail_smtp_timeout')
                                        ];

                                        $mail = new \Mail($this->config->get('config_mail_engine'), $mail_option);
                                        $mail->setTo($customer_info['email']);
                                        $mail->setFrom($from);
                                        $mail->setSender(html_entity_decode($store_name, ENT_QUOTES, 'UTF-8'));
                                        $mail->setSubject(html_entity_decode(sprintf($language->get('text_subject'), $store_name), ENT_QUOTES, 'UTF-8'));
                                        $mail->setText($this->load->view('mail/subscription_history', $data));
                                        $mail->send();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    // admin/controller/sale/subscription/addTransaction/after
    public function transaction(string &$route, array &$args, mixed &$output): void {
        if (isset($args[0])) {
            $subscription_id = $args[0];
        } else {
            $subscription_id = 0;
        }

        if (isset($args[1])) {
            $order_id = $args[1];
        } else {
            $order_id = 0;
        }

        if (isset($args[2])) {
            $comment = $args[2];
        } else {
            $comment = '';
        }

        if (isset($args[3])) {
            $amount = $args[3];
        } else {
            $amount = '';
        }

        if (isset($args[4])) {
            $type = $args[4];
        } else {
            $type = '';
        }

        if (isset($args[5])) {
            $payment_method = $args[5];
        } else {
            $payment_method = '';
        }

        if (isset($args[6])) {
            $payment_code = $args[6];
        } else {
            $payment_code = '';
        }

        // Subscription
        $this->load->model('sale/subscription');

        $filter_data = [
            'filter_subscription_id'        => $subscription_id,
            'filter_subscription_status_id' => $this->config->get('config_subscription_canceled_status_id'),
            'filter_date_next'              => date('Y-m-d H:i:s')
        ];

        $subscriptions = $this->model_checkout_subscription->getSubscriptions($filter_data);

        if ($subscriptions) {
            foreach ($subscriptions as $subscription) {
                $transaction_total = $this->model_sale_subscription->getTotalTransactions($subscription_id);

                if ($transaction_total) {
                    // Orders
                    $this->load->model('sale/order');

                    $order_info = $this->model_sale_order->getOrder($order_id);

                    // In this case, since we're canceling a subscription,
                    // the order ID needs to be identical
                    if ($order_info && $subscription['order_id'] == $order_info['order_id']) {
                        // Same for the payment method
                        if ($order_info['payment_method'] == $subscription['payment_method'] && $subscription['payment_method'] == $payment_method) {
                            // Same for the payment code
                            if ($order_info['payment_code'] == $subscription['payment_code'] && $subscription['payment_code'] == $payment_code) {
                                $this->load->language('mail/subscription');

                                // Store
                                $from = $this->config->get('config_email');
                                $store_name = $this->config->get('config_name');
                                $store_url = HTTP_CATALOG;
                                $alert_email = $this->config->get('config_mail_alert_email');

                                if ($comment) {
                                    $data['comment'] = nl2br($comment);
                                } else {
                                    $data['comment'] = '';
                                }

                                $data['subscription_id'] = $subscription_id;
                                $data['payment_method'] = $payment_method;
                                $data['payment_code'] = $payment_code;

                                $data['date_added'] = date($this->language->get('date_format_short'), $subscription['date_added']);

                                if ($this->config->get('config_mail_engine')) {
                                    $mail_option = [
                                        'parameter'     => $this->config->get('config_mail_parameter'),
                                        'smtp_hostname' => $this->config->get('config_mail_smtp_hostname'),
                                        'smtp_username' => $this->config->get('config_mail_smtp_username'),
                                        'smtp_password' => html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'),
                                        'smtp_port'     => $this->config->get('config_mail_smtp_port'),
                                        'smtp_timeout'  => $this->config->get('config_mail_smtp_timeout')
                                    ];

                                    $mail = new \Mail($this->config->get('config_mail_engine'), $mail_option);
                                    $mail->setTo($from);
                                    $mail->setFrom($from);
                                    $mail->setSender(html_entity_decode($store_name, ENT_QUOTES, 'UTF-8'));
                                    $mail->setSubject(html_entity_decode(sprintf($this->language->get('text_subject'), $store_name), ENT_QUOTES, 'UTF-8'));
                                    $mail->setText($this->load->view('mail/subscription_canceled', $data));
                                    $mail->send();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}