<?php

namespace Sibs\Stargate\Utils;

use Sibs\Stargate\Core\SibsGeneralFunctions;
use Sibs\Stargate\Models\SibsGeneralModels;

defined('ABSPATH') || exit;

class SibsNotes
{
    /**
     * SIBS Add Customer Note.
     */
    public static function sibs_add_customer_note(\WC_Order $wc_order)
    {
        $customer_note = $wc_order->get_customer_note();

        $new_line = "\n";
        $payment_comments = SibsGeneralFunctions::sibs_translate_frontend_payment($wc_order).$new_line;

        if (!empty($wc_order->get_customer_note())) {
            $customer_note .= $new_line;
        }

        $customer_note .= html_entity_decode($payment_comments, ENT_QUOTES, 'UTF-8');
        $order_notes = [
            'ID' => $wc_order->get_id(),
            'post_excerpt' => $customer_note,
        ];
        wp_update_post($order_notes);
    }

    /**
     * SIBS Add Order Notes.
     */
    public static function sibs_add_order_notes(\WC_Order $wc_order, array $status_name)
    {
        $user = get_user_by('id', get_current_user_id());

        $comments['order_id'] = $wc_order->get_id();
        $comments['author'] = $user->display_name;
        $comments['email'] = $user->user_email;
        $comments['content'] = sprintf(
            // translators: Original Status, New Status
            __('Order status changed from %1$s to %2$s', 'sibs-woo-stg'),
            $status_name['original'],
            $status_name['new']
        );

        SibsGeneralModels::save_order_notes($comments);
    }
}
