<?php

namespace Repetito\Admin;

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Repetito\Traits\OrderUtils;
use Repetito\Traits\Singleton;
use function wc_get_order;
use function wc_price;

final class OrdersListColumn {
    use Singleton;
    use OrderUtils;

    private const BOX_ICON = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" style="vertical-align: middle; margin-left: 4px;">
        <path d="M3 7.5L12 3L21 7.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
        <path d="M3 7.5V16.5L12 21L21 16.5V7.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
        <path d="M12 3V21" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
        <path d="M3 7.5L21 7.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
    </svg>';

    public function __construct() {
        // Classic post list view
        add_filter('manage_edit-shop_order_columns', array($this, 'addRepetitoFeeColumn'));
        add_action('manage_shop_order_posts_custom_column', array($this, 'displayRepetitoFeeColumn'), 10, 2);
        add_filter('manage_edit-shop_order_sortable_columns', array($this, 'makeRepetitoFeeColumnSortable'));

        // HPOS view
        add_filter('manage_woocommerce_page_wc-orders_columns', array($this, 'addRepetitoFeeColumn'));
        add_action('manage_woocommerce_page_wc-orders_custom_column', array($this, 'displayRepetitoFeeColumnHpos'), 10, 2);
        add_filter('manage_woocommerce_page_wc-orders_sortable_columns', array($this, 'makeRepetitoFeeColumnSortable'));

        // Add styles for the icon
        add_action('admin_enqueue_scripts', array($this, 'addStyles'));
    }

    /**
     * Add styles for the box icon
     */
    public function addStyles() {
        wp_register_style('repetito-orders-column', false);
        wp_enqueue_style('repetito-orders-column');
        wp_add_inline_style(
            'repetito-orders-column',
            '.repetito-fee-box {
                display: inline-flex;
                align-items: center;
                background-color: #edfaef;
                border: 1px solid #68a521;
                border-radius: 4px;
                padding: 2px 8px;
                color: #1e1e1e;
            }
            .repetito-fee-box-icon {
                color: #68a521;
                display: inline-block;
            }
            .repetito-fee-box-icon:hover {
                color: #4f7a19;
            }'
        );
    }

    /**
     * Add Repetito fee column to WooCommerce orders list
     */
    public function addRepetitoFeeColumn($columns) {
        $new_columns = array();
        foreach ($columns as $column_name => $column_info) {
            $new_columns[$column_name] = $column_info;
            if ('order_total' === $column_name) {
                $new_columns['repetito_fee'] = __('Repetito Fee', 'repetito');
            }
        }
        return $new_columns;
    }

    /**
     * Display Repetito fee value in the column (classic view)
     */
    public function displayRepetitoFeeColumn($column, $order_id) {
        if ('repetito_fee' === $column) {
            $order = wc_get_order($order_id);
            $this->displayFeeValue($order);
        }
    }

    /**
     * Display Repetito fee value in the column (HPOS view)
     */
    public function displayRepetitoFeeColumnHpos($column, $order) {
        if ('repetito_fee' === $column) {
            $this->displayFeeValue($order);
        }
    }

    /**
     * Display the fee value for an order
     */
    private function displayFeeValue($order) {
        $fee = $this->orderHasFee($order);
        $fee_amount = $fee ? $fee->get_total() : 0;
        if ($fee) {
            echo '<span class="repetito-fee-box">';
            echo wp_kses_post(wc_price($fee_amount));
            echo wp_kses_post('<span class="repetito-fee-box-icon" title="' . esc_attr__('Repetito reusable packaging', 'repetito') . '">' . self::BOX_ICON . '</span>');
            echo '</span>';
        } else {
            echo '—';
        }
    }

    /**
     * Make the Repetito fee column sortable
     */
    public function makeRepetitoFeeColumnSortable($columns) {
        $columns['repetito_fee'] = 'repetito_fee';
        return $columns;
    }
}