<?php

namespace WKO\Controllers;

defined('ABSPATH') || exit;

use WKO\Controllers\Emails\AwaitingAnalysisOrderEmail;

/**
 * Name: Emails
 * Handle custom WooCommerce emails
 * @package Controllers
 * @since 1.0.0
 */
class Emails
{
    public function __construct()
    {
        add_action('woocommerce_email_classes', [$this, 'add_triggers_to_default_emails'], 999);
        add_filter('woocommerce_email_actions', [$this, 'add_email_actions'], 10, 1);
    }

    /**
     * Register custom email classes
     *
     * @since 1.0.0
     * @param array $email_classes
     * @return array
     */
    public static function register_email_classes($email_classes)
    {
        $email_classes['WC_Awaiting_Analysis_Order_Email'] = new AwaitingAnalysisOrderEmail();

        return $email_classes;
    }

    /**
     * Add email actions for custom status
     *
     * @since 1.0.0
     * @param array $actions
     * @return array
     */
    public static function add_email_actions($actions)
    {
        $actions[] = 'woocommerce_order_status_pending_to_awaiting-analysis';
        $actions[] = 'woocommerce_order_status_failed_to_awaiting-analysis';
        $actions[] = 'woocommerce_order_status_cancelled_to_awaiting-analysis';
        $actions[] = 'woocommerce_order_status_awaiting-payment_to_awaiting-analysis';
        $actions[] = 'woocommerce_order_status_on-hold_to_awaiting-analysis';
        $actions[] = 'woocommerce_order_status_processing_to_awaiting-analysis';

        $actions[] = 'woocommerce_order_status_awaiting-analysis_to_processing';
        $actions[] = 'woocommerce_order_status_awaiting-analysis_to_completed';
        $actions[] = 'woocommerce_order_status_awaiting-analysis_to_on-hold';

        return $actions;
    }

    /**
     * Add triggers to default WooCommerce email classes for awaiting-analysis transitions
     *
     * @since 1.0.0
     * @param array $email_classes
     * @return array
     */
    public function add_triggers_to_default_emails($email_classes)
    {
        if (isset($email_classes['WC_Email_Customer_Processing_Order'])) {
            add_action('woocommerce_order_status_awaiting-analysis_to_processing_notification', [
                $email_classes['WC_Email_Customer_Processing_Order'],
                'trigger'
            ], 10, 2);
        }

        if (isset($email_classes['WC_Email_Customer_Completed_Order'])) {
            add_action('woocommerce_order_status_awaiting-analysis_to_completed_notification', [
                $email_classes['WC_Email_Customer_Completed_Order'],
                'trigger'
            ], 10, 2);
        }

        if (isset($email_classes['WC_Email_Customer_On_Hold_Order'])) {
            add_action('woocommerce_order_status_awaiting-analysis_to_on-hold_notification', [
                $email_classes['WC_Email_Customer_On_Hold_Order'],
                'trigger'
            ], 10, 2);
        }

        if (isset($email_classes['WC_Email_New_Order'])) {
            add_action('woocommerce_order_status_awaiting-analysis_to_processing_notification', [
                $email_classes['WC_Email_New_Order'],
                'trigger'
            ], 10, 2);
            add_action('woocommerce_order_status_awaiting-analysis_to_completed_notification', [
                $email_classes['WC_Email_New_Order'],
                'trigger'
            ], 10, 2);
            add_action('woocommerce_order_status_awaiting-analysis_to_on-hold_notification', [
                $email_classes['WC_Email_New_Order'],
                'trigger'
            ], 10, 2);
        }

        return $email_classes;
    }
}
