<?php

namespace Rtc\PlantShopWC\Emails;

use Rtc\PlantShopWC\Admin\Helpers\MiscHelper;
use Rtc\PlantShopWC\Admin\Models\GeneralSettingsModel;
use Rtc\PlantShopWC\Admin\Modules\BotanicalName;
use Rtc\PlantShopWC\Admin\Modules\PlantInstructions;
use Rtc\PlantShopWC\Admin\Views\Templates\Instructions;
use Rtc\PlantShopWC\Frontend\Models\CartModel;
use Rtc\PlantShopWC\Frontend\Views\CartView;
use WC_Order;
use function Rtc\PlantShopWC\Admin\Views\Templates\print_instructions__premium_only;
class EmailService {
    protected $model;

    protected $view;

    protected $checked;

    public function __construct() {
        $this->model = new CartModel();
        $this->view = new CartView();
        $opts = get_option( GeneralSettingsModel::get_option_key(), array() );
        $checked = ( isset( $opts['botanical_name_replacement'] ) ? (string) $opts['botanical_name_replacement'] : '' );
        $this->checked = $checked === '1';
    }

    public function register_hooks() {
        if ( $this->checked ) {
            add_filter(
                'woocommerce_order_item_get_name',
                array($this, 'plantshop_wc_order_email_products_title'),
                20,
                2
            );
        }
    }

    public function plantshop_wc_order_email_products_title( $name, $item ) {
        if ( !MiscHelper::is_email_context() ) {
            return $name;
        }
        $product = $item->get_product();
        if ( !$product || !GeneralSettingsModel::is_product_included( $product ) ) {
            return $name;
        }
        return ( new BotanicalName() )->build( $product, $name, true );
    }

}
