<?php

namespace WKO\Services\Koin\Requests\Orders;

use WKO\Controllers\Logs;
use WKO\Model\PostMeta;
use WKO\Services\Koin\Requests\InterfaceRequest;
use WKO\Services\Koin\Requests\Request;

/**
 * Name: Refund
 * Refund a new koin payment
 * @package Koin\Requests\Orders
 * @param array $body
 * @since 1.0.0
 */
class Refund extends Request implements InterfaceRequest
{
    private $order_id;
    private $koin_order_id;
    private $logs;
    private $post_m;

    public function __construct($body)
    {
        $this->post_m = new PostMeta;
        $this->logs = new Logs;
        $this->order_id = $body['order_id'];
        $this->koin_order_id = $this->post_m->get($body['order_id'], '_order_id');
        $this->header   = [];
    }

    public function process($body)
    {
        $wc_order = wc_get_order($this->order_id);

        if (!$this->order_id || !$this->koin_order_id) {
            return [
                'error' => true,
                'message' => __('Invalid order ID or Koin order ID', 'wc-koin-official')
            ];
        }

        $response = $this->refund_koin_order($this->koin_order_id, $body);

        if (isset($response['body'])) {
            $body = json_decode($response['body']);

            if (isset($body->refund_id)) {
                $this->handle_successful_refund($wc_order, $body);
                return [
                    'success' => true
                ];
            }
            if (isset($body->code)) {
                return $this->handle_refund_error($wc_order, $body);
            }
        }

        return [
            'error' => true,
            'message' => __('Error', 'wc-koin-official')
        ];
    }

    private function handle_successful_refund($wc_order, $body)
    {

        $title = __("==== KOIN REFUND SUCCESS ====", 'wc-koin-official');
        $message = sprintf(
            "%s %s",
            __("The refund was processed successfully.", 'wc-koin-official'),
            "\nBody request:\n" . print_r($body, true) . "\n\n"
        );

        $wc_order->add_order_note(sprintf(
            "<strong>%s</strong> : %s",
            WKO_PLUGIN_NAME,
            __("The refund worked successfully.", 'wc-koin-official')
        ), false);

        $logs = new Logs;
        $logs->refund_order_success($title, $message);

        return true;
    }

    private function handle_refund_error($wc_order, $body)
    {
        $logs = new Logs;
        $title = __("==== KOIN REFUND ERROR ====", 'wc-koin-official');
        $error_message = $body->message;
        $message = sprintf(
            "%s %s",
            $error_message,
            "\nBody request:\n" . print_r($body, true) . "\n\n"
        );

        $wc_order->add_order_note(sprintf(
            "<strong>%s</strong> : %s",
            WKO_PLUGIN_NAME,
            $error_message
        ), false);

        $logs->refund_order_error($title, $message);

        return [
            'error' => true,
            'message' => $error_message
        ];
    }

    public function refund_koin_order($koin_order_id, $body)
    {
        $this->endpoint = $this->get_endpoint('/payment/v1/orders/' . $koin_order_id . '/refund');
        $this->body = [
            'transaction' => $body['transaction'],
            'amount' => $body['amount']
        ];
        $this->header   = [];

        $this->method  = 'PUT';
        return $this->handle_request();
    }

    public function handle_request()
    {
        return $this->send();
    }
}
