<?php
/**
 * Filters.php
 *
 * @since      1.0.0
 * @author     alfiopiccione <alfio.piccione@gmail.com>
 * @license    http://opensource.org/licenses/gpl-2.0.php GPL v2
 *
 * Copyright (C) 2025 POP
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace WooPoPToFattureInCloudStock;

if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

/**
 * Class Filters
 *
 * @author alfiopiccione <alfio.piccione@gmail.com>
 */
class Filters
{
    /**
     * getProductsAction
     *
     * @param $output
     *
     * @return string
     */
    public static function getProductsAction($output)
    {
        $userAndCompanies = \WooPoPToFattureInCloud\Functions\getUserAndCompanies();
        if (isset($userAndCompanies->companyPermissions['stock']) && 'write' === $userAndCompanies->companyPermissions['stock']) {
            $output .= sprintf("<label class='products'><h3>%s:</h3><strong>%s</strong>",
                __('Stock management', WP_FATT_CLOUD_STOCK_TEXTDOMAIN),
                __('Get products from Fatture in Cloud', WP_FATT_CLOUD_STOCK_TEXTDOMAIN)
            );
            $output .= "<div class='products-list-action'>";
            $output .= sprintf('<a id="api_get_products" href="javascript:;" class="button button-primary">%s</a>',
                __('Get or Update products', WP_FATT_CLOUD_STOCK_TEXTDOMAIN)
            );
            $output .= sprintf('<p class="update_time">%s: <time>%s</time></p>',
                __('Last update', WP_FATT_CLOUD_STOCK_TEXTDOMAIN),
                get_option('wfc_ms_products_last_update_time') ?: ''
            );
            $output .= "</div>";
            $output .= "</label>";
        }

        return $output;
    }

    /**
     * setItemListProductID
     * Filter product id for set FIC product ID
     * @hooked "wfc-invoice-items_list-product_id"
     *
     * @param $prodID
     * @param $prodName
     * @param $prodCode
     *
     * @return int|mixed
     */
    public static function setItemListProductID($prodID, $prodName, $prodCode)
    {
        if ($prodID && $prodName && $prodCode) {
            $wcProduct = wc_get_product($prodID);
            if ($wcProduct instanceof \WC_Product) {
                $prodID = (int)get_post_meta($wcProduct->get_id(), '_manage_product_stock', true);
            }
        }

        return $prodID;
    }

    /**
     * setItemListProductCategory
     * Filter product id for set FIC product category
     * @hooked "wfc-invoice-items_list-product_category"
     *
     * @param $category
     * @param $prodID
     *
     * @return mixed|string
     */
    public static function setItemListProductCategory($category, $prodID)
    {
        if ($prodID) {
            $wcProduct = wc_get_product($prodID);
            if ($wcProduct instanceof \WC_Product) {
                $category = get_post_meta($wcProduct->get_id(), '_manage_product_stock_category', true);
            }
        }

        if (! $category) {
            return '';
        }

        return $category;
    }
}
